Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to input user values multi-dimensional arrays

 
anise hollingshead
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How do you input (prompt) values from users for multi-dimensional arrays? For example, if I wanted users to enter numbers into a 5 row, 2 column array to fill that array.

Thanks!
Anise
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

It sounds like your question is mostly about how to prompt for user input and then get that input. For that, check the java.util.Scanner class.

For valuing elements in the multidimensional array, I expect that you will want to use a nested loop, with each loop iterating over a particular dimension of the array. (For example, an outer loop for "columns," and an inner loop for "rows.")

Show us the code you have so far, and tell us where you're stuck.
 
anise hollingshead
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I know how to code prompts for user input in a general sense with the scanner object, but am not sure how to input data for multi-dimensional arrays.

I don't know if the data can be input all at once for all columns/rows, or if the data has to be input from the users separately for the rows, then for the columns.

I have numbers stated directly so far, but now want to have users input their own numbers for use in the program.

import java.util.Scanner;

public class Students1
{
static int [][]coffeeArray2 = { {1,2}, {2,3}, {3,4}, {4,5}, {5,6} };
static Scanner myInput = new Scanner( System.in );

public static void main (String [] args)
{
System.out.println( "Display my bins:" );
displayArray( coffeeArray2 );
System.out.println( "The Total lbs in bins is:" + calcTotalCoffee( coffeeArray2 ));

}
public static void displayArray( int coffeeArray2[][] )
{

for( int i = 0; i< coffeeArray2.length; i ++)
{
for(int j = 0; j< coffeeArray2[i].length; j ++)
System.out.printf( "%d ", coffeeArray2[i][j] );
System.out.println();

}

}
public static int calcTotalCoffee( int coffeeArray2[][] )
{
int total = 0;
for (int i = 0; i < coffeeArray2.length; i++)
{
for (int j = 0; j < coffeeArray2[i].length; j++)
total = total + coffeeArray2[i][j];

}
return total;
}
}


Thanks,
Anise
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This loop you already have:

works through an array of arrays very nicely. To ask the user to load the arrays, replace the printf with

Does that do what you need?
 
anise hollingshead
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but it doesn't really help. I need the actual code. This is what I have so far for inputs:

for(int i = 0; i < coffeeArray2.length; i ++)
{
for(int j = 0; j < coffeeArray2[i].length; j ++)

System.out.print("Enter number of lbs of coffee for the first aisle:");
coffeeArray2[i][j] = myInput.nextInt();

But this only gets me one column, the other column is 0's. How do you code for input for both columns?

Thanks,
Anise
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could be the way you've posted it, but does the line print twice, before you enter a number

Enter number of lbs of coffee for the first aisle:
Enter number of lbs of coffee for the first aisle:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic