7 Replies - 313 Views - Last Post: 06 June 2012 - 08:23 PM Rate Topic: -----

#1 mrhemphill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 16-May 12

Printing an Array

Posted 06 June 2012 - 03:39 PM

i am attempting to print an array i constructed with user inputs but i may have messed creating it. I need to either know how fix my array or print it out. help please :)

import java.util.Arrays;
import java.io.*;
class Average
{
   public static void main (String[] args)throws IOException
   {
      InputStreamReader inStream = new InputStreamReader (System.in);  
      BufferedReader stdin = new BufferedReader (inStream);  
      
      String num;
      int i = 1;
      int k = 1;
      int [][] a1 = new int [i][k];
      do
      { 
         System.out.println("Enter integer for column " + i + ", row " + k);
          num = stdin.readLine ( );
          
         k++;
      }
   while (k <= 4);
      k=k-4;
      ++i;
      do
      { 
          System.out.println("Enter integer for column " + i + ", row " + k);
          num = stdin.readLine ( );
          
         k++;
      }
   while (k <= 4);
       k=k-4;
      ++i;
      do
      { 
          System.out.println("Enter integer for column " + i + ", row " + k);
          num = stdin.readLine ( );
          
         k++;
      }
   while (k <= 4);
             k=k-4;
      ++i;
      do
      { 
          System.out.println("Enter integer for column " + i + ", row " + k);
          num = stdin.readLine ( );
          
         k++;
      }
   while (k <= 4);
       k=k-4;
      ++i;
      do
      { 
          System.out.println("Enter integer for column " + i + ", row " + k);
          num = stdin.readLine ( );
          
         k++;
      }
   while (k <= 4);
   System.out.println( Arrays.deepToString(a1));
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Printing an Array

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10777
  • View blog
  • Posts: 40,123
  • Joined: 27-December 08

Re: Printing an Array

Posted 06 June 2012 - 03:44 PM

You've messed up how? What specific problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Printing an Array

Posted 06 June 2012 - 03:44 PM

Where do you assign the user's values to the array? It would look something like:

a1[i][k] = num;  // you'll have to place so that
                 // incrementing the indices works correctly

Was This Post Helpful? 0
  • +
  • -

#4 mrhemphill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 16-May 12

Re: Printing an Array

Posted 06 June 2012 - 04:00 PM

View PostGregBrannon, on 06 June 2012 - 03:44 PM, said:

Where do you assign the user's values to the array? It would look something like:

a1[i][k] = num;  // you'll have to place so that
                 // incrementing the indices works correctly

When i place it, i recieve an error that points to num and says an int is required?
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Printing an Array

Posted 06 June 2012 - 04:02 PM

Show your updated code and your error message/stack trace - just like always.

This post has been edited by GregBrannon: 07 June 2012 - 01:40 AM

Was This Post Helpful? 0
  • +
  • -

#6 mrhemphill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 16-May 12

Re: Printing an Array

Posted 06 June 2012 - 08:05 PM

This is my modified code. I dont have any errors but its stuck in a loop. once ive inputed row 4 of column 5 it restarts? how can i stop this

import java.util.Arrays;
import java.io.*;
class Average
{
   public static void main (String[] args)throws IOException
   {
      InputStreamReader inStream = new InputStreamReader (System.in);  
      BufferedReader stdin = new BufferedReader (inStream);  
      String inData;
      int num;
      int row = 5;
      int col = 6;
      int ar[][] = new int [row][col];
      int i=1;
      int j=1;

for ( j = 1; j <6; j++)
{
for(i= 1;i <ar.length;i++)
{
System.out.println ("Input an integer for row "+i+ ", column "+j+":"); 
inData = stdin.readLine( );
 ar [i][j]= Integer.parseInt (inData);
}
}
for ( j = 1; j <6; j++)
{
for(i= 1;i<ar.length;i++)
{
System.out.println ("Input an integer for row "+i+ ", column "+j+":"); 
inData = stdin.readLine( );
ar [i][j]= Integer.parseInt (inData);
}
}
for ( j = 1; j <6; j++)
{
for(i = 1;i <ar.length;i++)
{
System.out.println ("Input an integer for row "+i+ ", column "+j+":"); 
inData = stdin.readLine( );
ar [i][j]= Integer.parseInt (inData);
}
}
for ( j = 1; j <6; j++)
{
for(i = 1;i<ar.length;i++)
{
System.out.println ("Input an integer for row "+i+ ", column "+j+":"); 
inData = stdin.readLine( );
ar [i][j]= Integer.parseInt (inData);
}
}
for ( j = 1; j <6; j++)
{
for(i = 1;i <ar.length;i++)
{
System.out.println ("Input an integer for row "+i+ ", column "+j+":"); 
inData = stdin.readLine( );
ar [i][j]= Integer.parseInt (inData);
}
}
for ( j = 1; j <6; j++)
{
for(i = 1;i <ar.length;i++)
{
System.out.println ("Input an integer for row "+i+ ", column "+j+":"); 
inData = stdin.readLine( );
ar [i][j]= Integer.parseInt (inData);

}
}

}
}


Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7958
  • View blog
  • Posts: 13,564
  • Joined: 19-March 11

Re: Printing an Array

Posted 06 June 2012 - 08:19 PM

View Postmrhemphill, on 06 June 2012 - 06:00 PM, said:

View PostGregBrannon, on 06 June 2012 - 03:44 PM, said:

Where do you assign the user's values to the array? It would look something like:

a1[i][k] = num;  // you'll have to place so that
                 // incrementing the indices works correctly

When i place it, i recieve an error that points to num and says an int is required?



you've declared num as a String here, and this is an array of int. When someone enters a number, you're going to get it as a String. You need to then convert it from the String representation to an int. Use the parseInt method of the Integer class to do this.


EDIT: okay, you've sorted that. Next problem...

Quote

once ive inputed row 4 of column 5 it restarts? how can i stop this


Well, to be fair, you do ask it to.

You have the input loop repeated 6 times, which means that you're going to enter the whole array six times, and only the last one will count.

This post has been edited by jon.kiparsky: 06 June 2012 - 08:22 PM

Was This Post Helpful? 0
  • +
  • -

#8 mrhemphill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 16-May 12

Re: Printing an Array

Posted 06 June 2012 - 08:23 PM

so how would i go about correcting this?


After an hour of failing miserably i now see my error . thank you for your help.

This post has been edited by mrhemphill: 06 June 2012 - 08:29 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1