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 Reading input from console to 2D array with Scanner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading input from console to 2D array with Scanner Class" Watch "Reading input from console to 2D array with Scanner Class" New topic
Author

Reading input from console to 2D array with Scanner Class

Sohail Hasware
Greenhorn

Joined: Mar 19, 2012
Posts: 16
Hi,

I am very new to Java and this is my first attempt with arrays. I am trying to read in values for a 2D array with the Scanner Class which I have used previously. The following is my code:



Thanks
-Sohail
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

And what specific problems are you having?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40038
    
  28
There is no such thing as a 2D array. Only an array of arrays.
Sohail Hasware
Greenhorn

Joined: Mar 19, 2012
Posts: 16
Well, I know that the pseudo code I wrote is completely wrong. And sorry for calling it a 2D array and not a array of arrays. I want the user to fill the MobilePh with the index0, then the HomePh with the index 1 and finally OtherPh with the index 2. I am getting the following errors if I just take line nr 12-13 as the other errors are of a similar cause.

Description Resource Path Location Type
Syntax error on token ""Enter Mobile Phone nr: "", delete this token PhoneBook.java /Contacts/src line 12 Java Problem
Description Resource Path Location Type
Syntax error on token(s), misplaced construct(s) PhoneBook.java /Contacts/src line 12 Java Problem


Description Resource Path Location Type
Syntax error on token "(", Expression expected after this token PhoneBook.java /Contacts/src line 13 Java Problem

Description Resource Path Location Type
Syntax error on token "anArrayOfPhoneNrs", VariableDeclaratorId expected after this token PhoneBook.java /Contacts/src line 13 Java Problem

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40038
    
  28
Those error messages look like no Java™ error messages I have ever seen. Are you trying to write to a database and getting errors thence?
Sohail Hasware
Greenhorn

Joined: Mar 19, 2012
Posts: 16
No, no db is used, I just want to fill that array with the help of Scanner object
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40038
    
  28
You will need numbers in the [] after the name of the array.
It is usually bad design to use arrays of arrays of anything. You should set up PhoneNumber objects.
Sohail Hasware
Greenhorn

Joined: Mar 19, 2012
Posts: 16
Will those numbers be the index position in the array? For example:



I am still getting the same error though on that line.

So they should be set up as separate String variables you mean? The reason I am using 2 arrays is for pure exercise reason. But please why to use separate phone variables rather then arrays, I would like to learn why?
Sohail Hasware
Greenhorn

Joined: Mar 19, 2012
Posts: 16
Sorry, I forgot to apply some line of code before, mainly to start the Scanner class, the errors for the array are the same though. Here is the new code:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40038
    
  28
As you will have realised, putting data into parallel arrays or [][] arrays makes it difficult to match the locations. A PhoneNumber class will retain the numbers for landlines mobiles and faxes and can be associated with a person easily. You can do anything you like under the justification of
I only want to see what happens if . . .
however, as you appear to be doing.
Michal Plutynski
Greenhorn

Joined: Mar 01, 2012
Posts: 14

Hi,
Of course Campbell is right, but maybe (only for learning purpouses) we can stay with array of arrays.
Please take a look here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html - there is nice "MultiDimArrayDemo" example.

The next step will be to check here: docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html
there are also nice example of using Scanner methods.

Then try to correct your code and when you have more problems just put your code here and write what are your exceptions.

Regards.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading input from console to 2D array with Scanner Class