Author |
Message |
Justin_
|
Posted: Mon Mar 06, 2006 3:45 pm Post subject: readLine() question |
|
|
If you expected the user to input an integer what is the best way?
the readLine() method only accepts strings, I know you could make a string into an int, but is that the only way?
And how can I declare an array in java? |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Justin_
|
Posted: Mon Mar 06, 2006 3:47 pm Post subject: (No subject) |
|
|
I have to output a chart for an assignment, and we haven't touched on arrays yet... Unless there's an easier way in java, I need to learn how to create an array otherwise this chart is going to be a living hell. |
|
|
|
|
 |
wtd
|
Posted: Mon Mar 06, 2006 4:28 pm Post subject: (No subject) |
|
|
I suggest thst for your first question about converting a String to an int you see the Integer class.
As for arrays, I suggest you see my tutorial, and use your browser's find to search for "Arrays". |
|
|
|
|
 |
[Gandalf]

|
Posted: Mon Mar 06, 2006 4:48 pm Post subject: (No subject) |
|
|
Where are you getting the readLine() method from? Whatever it is, there should be a readInt() method in the same library. Use that instead of converting the string to an int. |
|
|
|
|
 |
wtd
|
Posted: Mon Mar 06, 2006 4:52 pm Post subject: (No subject) |
|
|
[Gandalf] wrote: Where are you getting the readLine() method from? Whatever it is, there should be a readInt() method in the same library. Use that instead of converting the string to an int.
He's almost certainly wrapping System.in in a BufferedReader instance, in which case no, there is no readInt method. |
|
|
|
|
 |
Justin_
|
Posted: Mon Mar 06, 2006 6:25 pm Post subject: (No subject) |
|
|
Yes wtd is correct.
And I know about the Integer class and was hoping there was a way to do without it. |
|
|
|
|
 |
[Gandalf]

|
Posted: Mon Mar 06, 2006 6:53 pm Post subject: (No subject) |
|
|
Yes. In that case, scrap the BufferedReader, and replace it with the Scanner class. Much more appropriate. |
|
|
|
|
 |
Andy
|
Posted: Tue Mar 07, 2006 8:59 am Post subject: (No subject) |
|
|
use StringTokenizer to find the integer, and then parse it |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
|