
-----------------------------------
Prince Pwn
Sun Feb 07, 2010 9:22 pm

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?


/***********************************

    Program:    Question 6.26
    Date:       February 7, 2010

***********************************/



public class Q626
{
    public static String invert (int val)
    {
        String pass = "";
        String result = "";
        for (int i = val ; i >= 1 ; i /= 10)
        {
            pass += i;
            result += pass.toCharArray () 

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? 


/***********************************

    Program:    Question 6.26
    Date:       February 7, 2010

***********************************/

import hsa.Console;
import java.io.*;

public class Q626
{

    static Console c;

    public static String invert (int val)
    {
        String pass = "";                       // Holds numer as it passes through loop modifications
        String result = "";                     // Carry's the final result
        for (int i = val ; i >= 1 ; i /= 10)    // Goes through each digit
        {
            pass += i;                          // Adds current digit to pass
            result += pass.toCharArray () 

-----------------------------------
DemonWasp
Mon Feb 08, 2010 3:49 am

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 since part), so of course RTP doesn't know about it. You can use [url=http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedReader.html]a BufferedReader wrapped around an [url=http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStreamReader.html]InputStreamReader as follows:
[code]
 BufferedReader in
   = new BufferedReader(new InputStreamReader(System.in));
[/code]

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.

-----------------------------------
Prince Pwn
Tue Feb 09, 2010 12:55 pm

RE:Ready to Program; System.in
-----------------------------------
Q#1 

I ran:


        BufferedReader in = new BufferedReader (new InputStreamReader (System.in));


It doesn't error on me, it just passes right past it and doesn't allow user input.

Q#2


try                                 // Attempts to take a valid integer
{
     val = c.readInt ();                 // Takes user input
}
catch (Exception error)                 // If non-numeric or too large a number is entered
{
     c.println ("Please enter a numerical value less than " + Integer.MAX_VALUE);
}


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.

-----------------------------------
TerranceN
Tue Feb 09, 2010 3:51 pm

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

-----------------------------------
DemonWasp
Tue Feb 09, 2010 3:59 pm

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.

-----------------------------------
Prince Pwn
Tue Feb 09, 2010 4:43 pm

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:


/***********************************

    Program:    Question 6.26
    Date:       February 7, 2010

***********************************/

import java.io.*;

public class Q626
{
    public static String invert (int val)
    {
        String pass = "";                       // Holds numer as it passes through loop modifications
        String result = "";                     // Carry's the final result
        for (int i = val ; i >= 1 ; i /= 10)    // Goes through each digit
        {
            pass += i;                          // Adds current digit to pass
            result += pass.toCharArray () 
