wood burning stoves*
The moose likes Beginning Java and the fly likes reading number from System.in into array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reading number from System.in into array" Watch "reading number from System.in into array" New topic
Author

reading number from System.in into array

Peter Merker
Ranch Hand

Joined: Oct 18, 2003
Posts: 37
Hi everybody!
I'm trying to put user input in an array. Something is not working.
The input doesn't seem to be recognized by the program. e.g. 999 should become an int[3] array with 9 at each index position.
I'm using the following code for the input method, which I found in a book
and changed where commented.

Maybe this code is not appropiate for reading in a number from the command line into an integer array?
Peter
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Hi Peter
Do you want to read in just one number, or a series of numbers? Why do you need to put the individual digits of these numbers into an array? I assume you want to use this array to perform some manipulation of the individual digits - is this acurate? What is your overall goal?
Michael


"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
Peter Merker
Ranch Hand

Joined: Oct 18, 2003
Posts: 37
Hi Michael,
thanks for Your answer. I want to program a mastermind game. The user can try to guess the secret random number and each time will get a result for
i) digits at the right place
ii) digits at the wrong place
Therefore there need to be arrays with the guessed number and the random number to be compared. I wrote most of the code already, though I'm not sure if it will work. It compiles o.k., but at the moment I get an exception even before it is possible to enter anything. It seems that the program hops right to the next command and ignores the input command, which results in an exception.
Peter
Peter Merker
Ranch Hand

Joined: Oct 18, 2003
Posts: 37
Well, I should have taken out the command "reader.close();". Now the input works, but I still got lots of problems with this program... :-{
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Peter
Have a look at my post on this thread. This will enable you to read in each character of the input number one by one. You can then store them in whatever data structure you think appropriate. You could even compare them against the 'mastermind' number as they are read in from System.in, if you like.
One way to store the mastermind sequence (and the user's input) would be a String - you could store words as well as numbers. Check out the String.charAt() method to figure out how to perform the comparisons of individual characters within the Strings.
If you use arrays, you are almost reinventing the wheel - the String class can be thought of as an array of chars and a set of operations you can perform against it.
Hope this helps
Michael
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: reading number from System.in into array
 
Similar Threads
FileIO and mult. arrays
Array question
problem in loop
Java input
Please help on Strings