Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 readLine() question
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Justin_




PostPosted: 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
Sponsor
sponsor
Justin_




PostPosted: 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




PostPosted: 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]




PostPosted: 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




PostPosted: 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_




PostPosted: 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]




PostPosted: 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




PostPosted: Tue Mar 07, 2006 8:59 am   Post subject: (No subject)

use StringTokenizer to find the integer, and then parse it
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: