Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

New To Java - Input Number Won't Prompt, Wrong Result

A simple program that compiled success, but can not input second number, no prompt. number doesn't match the first number input at keyboard with display on output. Thanks

========================
/* Enter two number at prompt
* Add both number
* Print out Total
*
* 1/31/2011
* Code Name Add2Number.java
*/

import java.io.*;

class Add2Number {
public static void main(String[] args)
throws java.io.IOException {
int n1, n2, Total;

System.out.println("Enter Your First Number:");
n1 = (int) System.in.read();
System.out.println("Enter Your Second Number:");
n2 = (int) System.in.read();

Total = n1 + n2;
System.out.println("n1 = " + n1);
System.out.println("n2 = " + n2);

System.out.println("Total = " + Total);
}
}

Comments

  • krishbalodakrishbaloda Member Posts: 45
    import java.io.*;

    class Add2Number {
    public static void main(String[] args)
    throws java.io.IOException {
    int n1=0, n2=0, Total;
    //create object of scanner
    Scanner kbd = new Scanner(System.in);

    System.out.println("Enter Your First Number:");

    //change here

    n1 = kbd.nextInt();

    System.out.println("Enter Your Second Number:");

    //change here

    n2 = kbd.nextInt();


    Total = n1 + n2;
    System.out.println("n1 = " + n1);
    System.out.println("n2 = " + n2);

    System.out.println("Total = " + Total);
    }
    }

    this will work fine

  • jhuwalkerjhuwalker Member Posts: 5
    Thanks krishbaloda ! the scanner is working
Sign In or Register to comment.