# 2D array input

Printable View

• September 13th, 2013, 06:24 PM
Rain_Maker
2D array input
Here is the Lab information

Create a program that keeps track of the following information input by the user:
First Name, Last Name, Phone Number, Age

Now - let's store this in a multidimensional array that will hold 10 of these contacts. So our multidimensional array will need to be 10 rows and 4 columns.
You should be able to add, sort (by any column you chose) and remove contacts in the array.

Questions

I can code all the information provided that there are 1D arrays. But my Professor wants me to do them in 2D which is a little of challenge for me. My question is if my I do a 2D array will that be in conflict with m phone number and Age column?

--- Update ---

I was going to start with this.

Code Java:

``` for(int row = 0; row < demographic.length; row++) { for(int column = 0; column < demographic[row].length; column++) { demographic[row][column] = " "; System.out.print( demographic[row][column] ); } System.out.println(); }```

--- Update ---

Don't know why i can not edit my post but here is the beginning code.

Code Java:

``` Scanner scan = new Scanner(System.in); String[][] demographic = new String[10][4];       for(int row = 0; row < demographic.length; row++) { for(int column = 0; column < demographic[row].length; column++) { demographic[row][column] = " "; System.out.print( demographic[row][column] ); } System.out.println(); }```
• September 13th, 2013, 06:31 PM
GregBrannon
Re: 2D array input
I don't understand your question. What do you mean by conflict?

Another way to look at "multi-dimensional" arrays is to think of them as "arrays of arrays." In this case, the required 1D array looks like:

String[] contacts = { "FirstName", "LastName", "PhoneNumber", "Age" };

which is one row of 4 elements or columns. If there are to be multiple contacts, then there must be multiple rows, so that:

contacts[0] = { "FirstName0", "LastName0", "PhoneNumber0", "Age0" };
contacts[1] = { "FirstName1", "LastName1", "PhoneNumber1", "Age1" };
contacts[2] = { "FirstName2", "LastName2", "PhoneNumber2", "Age2" };

and so on . . .

Then to specify LastName1, the second element (or column) of the 2nd row:

contacts[1][1].

I'm sure you'll still have questions, so come back with more.
• September 13th, 2013, 06:37 PM
Rain_Maker
Re: 2D array input
GregBrannon,

Thank You so much for the quick response. What I mean by is Firstname and LastName are String type data. And my phone number and age is int type data. I'm just confuse on how I will declare this array. I was going to use a string type array but notice the last data types where int type. Any suggestions?
• September 13th, 2013, 06:41 PM
GregBrannon
Re: 2D array input
An array must contain the same data types. Do phone number and age have to be ints? If not, consider making all Strings. If they must be ints, you can convert back and forth from String to int (or Integer), but there can't be different types in the same array. Converting isn't a big deal and is done often as needed.
• September 13th, 2013, 06:45 PM
Rain_Maker
Re: 2D array input
okay.....This may sounds a little ignorant :rolleyes: but I never actually stored numbers as String type variables. Is there something I read or refer to.
• September 13th, 2013, 07:08 PM
GregBrannon
Re: 2D array input
"(123) 456-7890" is a string of numbers that could be a phone number. "32" is a string of numbers that could be someone's age. It's pretty simple, really. I can't think of a reference to point you to that explains it in greater detail, and I think the words would be wasted.
• September 13th, 2013, 07:16 PM
Rain_Maker
Re: 2D array input
ohh wow I never that of it like that lol Thanks!