-1

I'm new in java. I'm learning Java with book by Herbert Schildt. I want to write program that ask user to enter two variables, then ask to choose arithmetic operation and then program do this operation (like any calculator). I use this method - System.in.read from Schildts book, but my program only can ask user to input one variable. Programs doesn't ask user to input second variable. How can I do this?

My code below:

class Calc {
public static void main(String args[]) 

throws java.io.IOException {
    int a, b, result;
    System.out.print("Enter first digit ");
    a = (int) System.in.read();

    System.out.println("Enter second digit ");
    b = (int) System.in.read();

    System.out.println("Choose operation");
    System.out.println("Multiplication - 1 or Division - 2 ");

    //int r;
    result = (int) System.in.read();
        switch(result) {
            case 1:
            System.out.println(a * b);
            break;
            case 2:
            System.out.println(a / b);  
            break;
            default: System.out.println("incorrect digit");
        }
    }       
}   
Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
Kos
  • 31
  • 1
  • 2
  • 8
  • *"but my program only can ask user to input one variable"* - Because it doesn't need to ask, it still has enough in the input stream to read from. – Tom Jun 24 '17 at 10:21
  • see also https://stackoverflow.com/questions/15446689/what-is-the-use-of-system-in-read – John3136 Jun 24 '17 at 10:22
  • BTW It's weird that a book suggests to use `System.in.read();`, as it returns a single byte while an integer can take up to 4 bytes. This is really limiting. Also, you won't get the correct number from user input, you will get the byte representation of the character inputted by the user (e.g. `49` if the user inputs `1`). – BackSlash Jun 24 '17 at 10:22
  • Use scanner instead it is fast and easy to use – Lone_Coder Jun 24 '17 at 10:22
  • Back in my day,... we used `new BufferedReader(new InputStreamReader(System.in));` – cs95 Jun 24 '17 at 10:24
  • @BackSlash Not only that, you also need to _actively_ ignore the CR and LF input from the stream. Other classes do that for you. (Just an addition to what you've already wrote) – Tom Jun 24 '17 at 10:24
  • @Coldspeed uphill, both ways, in the snow. – Dawood ibn Kareem Jun 24 '17 at 10:42
  • A word of advice: drop Schildt's book; he is notorious for writing really bad programming books on languages he has barely any knowledge of. – Mark Rotteveel Jun 24 '17 at 10:46
  • @Mark Rotteveel What book you can recommend to me? I'm very beginner. I like how he (Schildt) describes in detail all parts of the code and gives tasks at the end of each chapter. – Kos Jun 24 '17 at 11:00

1 Answers1

-1
public static void main(String args[]) {

        int a, b, result;
        final Scanner sc = new Scanner(System.in);
        System.out.print("Enter first digit ");
        a = sc.nextInt();
        System.out.println("Enter second digit ");
        b = sc.nextInt();
        System.out.println("Choose operation");
        System.out.println("Multiplication - 1 or Division - 2 ");
        // int r;
        result = sc.nextInt();
        switch (result) {
            case 1:
                System.out.println(a * b);
                break;
            case 2:
                System.out.println(a / b);
                break;
            default:
                System.out.println("incorrect digit");
        }
    }
Nishant Bhardwaz
  • 1,119
  • 7
  • 18