Computer Science Canada Ready to Program; System.in |
Author: | Prince Pwn [ Sun Feb 07, 2010 9:22 pm ] | ||||
Post subject: | Ready to Program; System.in | ||||
When I do Scanner x = new Scanner (System.in) sp?, it tends to not want to run in RTP. "There's no Scanner" something to that matter. Is there any way to recieve input using RTP inside the System console without the HSA console?
Also instead of making a new thread since it's essentially the same application, how come my catch statement is never reached properly in this HSA port of the above program?
|
Author: | DemonWasp [ Mon Feb 08, 2010 3:49 am ] | ||
Post subject: | RE:Ready to Program; System.in | ||
First question: As I recall, RTP uses a version of Java that's pretty old (probably 1.4.2), and the Scanner class only appeared in 1.6 (see the since part), so of course RTP doesn't know about it. You can use a BufferedReader wrapped around an InputStreamReader as follows:
Second question: nothing in the try{} clause throws an Exception, so there's no reason for control to flow through the catch{} block. This really isn't the proper way to use an exception anyway, because this won't prompt the user to enter another number if they enter it incorrectly: you'd need a loop for that. |
Author: | Prince Pwn [ Tue Feb 09, 2010 12:55 pm ] | ||||
Post subject: | RE:Ready to Program; System.in | ||||
Q#1 I ran:
It doesn't error on me, it just passes right past it and doesn't allow user input. Q#2
What do you mean by putting something in the try claus? It's attempting to read the int and if it fails (ie. not numeric or > max val) then spit out error. |
Author: | TerranceN [ Tue Feb 09, 2010 3:51 pm ] |
Post subject: | RE:Ready to Program; System.in |
The hsa.Console handles its own errors, so c.readInt() does not thow any. You could just read a string from the console, then convert it manually, which can thow an error, which wil be handled by your program. Also, you need to put it in a loop, so your user can try again. Finally, it should say specifically that you want an integer, because 3.14 is also a numberical value, but it cannot be converted to an integer (from a string at least). Hope that helps |
Author: | DemonWasp [ Tue Feb 09, 2010 3:59 pm ] |
Post subject: | RE:Ready to Program; System.in |
Creating the BufferedReader just makes a BufferedReader object reading from the specified Reader (an InputStreamReader in this case). It won't block to wait for user input. You would have to invoke in.readLine() to get a String containing the user input, then try Integer.parseInteger ( inputString ) to get the integer value. The latter will throw a NumberFormatException if the value is not a valid integer. |
Author: | Prince Pwn [ Tue Feb 09, 2010 4:43 pm ] | ||
Post subject: | RE:Ready to Program; System.in | ||
Ah thanks so much. I could throw in a loop and handle errors better but the question doesn't ask for that and I have a few more questions to go. If I get some time I'll throw in better error handling and maybe throw in a loop for possible bonus marks. Here's my final code:
|