File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to input user values multi-dimensional arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to input user values multi-dimensional arrays" Watch "How to input user values multi-dimensional arrays" New topic
Author

How to input user values multi-dimensional arrays

anise hollingshead
Greenhorn

Joined: Sep 07, 2006
Posts: 4
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
anise hollingshead
Greenhorn

Joined: Sep 07, 2006
Posts: 4
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

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
anise hollingshead
Greenhorn

Joined: Sep 07, 2006
Posts: 4
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

Joined: Jun 09, 2003
Posts: 4632
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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to input user values multi-dimensional arrays