• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading input from console to 2D array with Scanner Class

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what specific problems are you having?
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no such thing as a 2D array. Only an array of arrays.
 
Sohail Hasware
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, no db is used, I just want to fill that array with the help of Scanner object
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 14
jQuery Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic