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

Username:   Password: 
 RegisterRegister   
 Ready to Program; System.in
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Prince Pwn




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

Java:

/***********************************

    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 () [pass.toCharArray ().length - 1];
        }
        return result;
    }


    public static void main (String[] args)
    {
        try
        {
            System.out.println ("Enter a number: ");
            // system.in here
        }
        catch (Exception error)
        {
            System.out.println ("Please enter a numerical value less than " + Integer.MAX_VALUE);
        }
    }
}


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?

Java:

/***********************************

    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 () [pass.toCharArray ().length - 1]; // Adds last digit to final result
        }
        return result;
    }


    public static void main (String[] args)
    {
        c = new Console ();

        int val = 0;
        c.print ("Enter a number: ");
        try
        {
            val = c.readInt ();
        }
        catch (Exception error)
        {
            c.println ("Please enter a numerical value less than " + Integer.MAX_VALUE);
        }
        c.println ("Number " + val + " inverted is " + invert (val));
    }
}
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




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

 BufferedReader in
   = new BufferedReader(new InputStreamReader(System.in));


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




PostPosted: Tue Feb 09, 2010 12:55 pm   Post subject: RE:Ready to Program; System.in

Q#1

I ran:

Java:

        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

Java:

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




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




PostPosted: 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.
Prince Pwn




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

Java:

/***********************************

    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 () [pass.toCharArray ().length - 1]; // Adds last digit to final result
        }
        return result;
    }


    public static void main (String[] args)
    {
        BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
        int val = 0;                            // For user input
        System.out.print ("Enter a number: ");            // Prompts user for input
        try                                     // Attempts to take a valid integer
        {
            val = Integer.parseInt (in.readLine ());
            System.out.println ("Number " + val + " inverted is " + invert (val));   // Displays inverted integer
        }
        catch (Exception error)                 // If non-numeric or too large a number is entered
        {
            System.out.println ("Please enter a numerical value less than " + Integer.MAX_VALUE);
        }
    }
}
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  [ 6 Posts ]
Jump to:   


Style:  
Search: