View Single Post
Old Jul 6th, 2007, 1:54 AM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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 is offline   Reply With Quote