Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Can't make the Scanner thing work (http://www.programmingforums.org/showthread.php?t=13484)

357mag Jul 6th, 2007 2:54 AM

Can't make the Scanner thing work
 
I'm using Java 6 and I'm trying to input two integers from the keyboard and then output the sum. Here is what I got:

:

import java.util.Scanner;

public class GettingInput {
   
    public static void main(String args[])throws IOException
    {
        Scanner s = new Scanner(System.in);
       
        System.out.print("Enter first integer: ");
        int x = s.nextInt();
       
        System.out.print("Enter second integer: ");
        int y = s.nextInt();
       
        int sum = x + y;
       
        System.out.println("The sum is " + sum);
    }
   
}


But when I run the program it says "cannot find symbol" and it highlights the public static void line. When I remove the throws IOException part, the program runs but it runs all wrong. It doesn't prompt me to enter a number, yet there still is the little box at the bottom to enter a number. If I enter the first number, and hit Enter, then enter the second number, then hit Enter, the program works, but then after all that it shows the prompts about entering the first integer and entering the second integer. It's all backwards.
Need a bit of help here.

357mag Jul 6th, 2007 3:17 AM

Well I guess if you change it to say println instead of just print then it works. Although I don't understand why.

I have another question though. Every time I run the program, the previous first integer value is still sitting in the input box. Is there any way to make it so when you start the program anew that the input box is clear?

lectricpharaoh Jul 6th, 2007 3:20 AM

I've run into a similar problem. It seems that the Java libraries (or possibly the VM) have a bug whereby the output stream and input stream are tied together. If you put a newline at the end of the prompts, or use println() instead of print(), it should work as you intend. I've tried flushing the output stream, to no avail. In my case, the bug manifested by the input not working unless it was printing a newline character; the symptom was an endless loop. Now, why changing what the output should affect the input is beyond me; hence my thinking that it's a bug. It also makes it impossible to have a proper prompt where the user enters stuff on the same line as the prompt.

[edit] I just tried your code; it works fine for me. Go figure. [/edit]
Quote:

Originally Posted by 357mag
Well I guess if you change it to say println instead of just print then it works. Although I don't understand why.

Hence it being a bug.
Quote:

Originally Posted by 357mag
I have another question though. Every time I run the program, the previous first integer value is still sitting in the input box. Is there any way to make it so when you start the program anew that the input box is clear?

The first time you said 'input box', I dismissed it, but now you've said it again. I don't know what you're referring to. Are you running this through some IDE, or something? If so, have you tried running it from the command line?

357mag Jul 6th, 2007 4:18 AM

I'm using Netbeans. Whenever you re-run the program, the previous first value that you entered is still sitting in the input box. But when I do the same program in JDeveloper, the input box is clear every time you re-run the program. I've been experimenting with lots of IDE's seeing which one I like best.

ReggaetonKing Jul 6th, 2007 9:16 AM

By the way, the IOException class is in the java.io package. You must import it in order to use that class. Just throwing it out there.

DaWei Jul 6th, 2007 9:44 AM

Run it from the command line, as lectric suggested. NetBeans is trapping your output and input to put it in the li'l windows, and getting it wrong.


All times are GMT -5. The time now is 2:32 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC