Computer Science Canada readLine() question |
Author: | Justin_ [ 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? |
Author: | Justin_ [ Mon Mar 06, 2006 3:47 pm ] |
Post 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. |
Author: | wtd [ Mon Mar 06, 2006 4:28 pm ] |
Post 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". |
Author: | [Gandalf] [ Mon Mar 06, 2006 4:48 pm ] |
Post 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. |
Author: | wtd [ Mon Mar 06, 2006 4:52 pm ] |
Post 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. |
Author: | Justin_ [ Mon Mar 06, 2006 6:25 pm ] |
Post subject: | |
Yes wtd is correct. And I know about the Integer class and was hoping there was a way to do without it. |
Author: | [Gandalf] [ Mon Mar 06, 2006 6:53 pm ] |
Post subject: | |
Yes. In that case, scrap the BufferedReader, and replace it with the Scanner class. Much more appropriate. |
Author: | Andy [ Tue Mar 07, 2006 8:59 am ] |
Post subject: | |
use StringTokenizer to find the integer, and then parse it |