-2

I'm trying to make a calculator in java that can multiply subtract and add depending if the user wants that they can choose what they want. For some reason its giving me a weird output Code

import java.util.Scanner;  // Import the Scanner class

public class calculator {
    public static void main(String args[]){
        Scanner sc= new Scanner(System.in);
            //System.in is a standard input stream  
        System.out.print("Enter first number- ");  
        int a = sc.nextInt();  
        System.out.print("Enter second number- ");  
        int b = sc.nextInt();  
        System.out.print("Do you want to multiply, add, divide, or subtract? ");  
        String c = sc.nextLine();  
        switch(c) {
            case "multiply":
              System.out.print(a * b);
              break;
            case "add":
              System.out.print(a * b);
              break;
            default:
              System.out.print("Invalid input!");
        }


    }
    
}

Output

Enter first number- 2
Enter second number- 2
Do you want to multiply, add, divide, or subtract? Invalid input!

Like I didnt even type Invalid input it just does it by itself for some reason

2 Answers2

0

There can be input left in the scanner before you request a value. In this case, the line break marks the end of the integer, but is not consumed as part of the integer. The call to nextLine() sees there is already an unused line break at the end of the buffer and returns that result. In this case, an empty string is returned. One way to fix this is to consume that unused line break first before requesting the next line or requesting a full line then parsing an integer from it.

Scanner scan  = new Scanner(System.in);

// Always request a full line
int firstInt = Integer.parse(scan.nextLine());
int secondInt = Integer.parse(scan.nextLine());
String option = scan.nextLine();

// Use an extra call to nextLine() to remove the line break causing the issues
int firstInt = scan.nextInt();
int secondInt = scan.nextInt();
scan.nextLine(); // Consume the unused line break
String option = scan.nextLine();
Locke
  • 7,626
  • 2
  • 21
  • 41
-1

sc.nextInt() does not read the enter key that you entered, so sc.nextLine() will read that new line and return it. Use sc.next() instead of sc.nextLine() to avoid this issue. Your code also multiplies the numbers when the user inputs add, so I changed that as well.

import java.util.Scanner;  // Import the Scanner class

public class calculator {
    public static void main(String args[]){
        Scanner sc= new Scanner(System.in);
        //System.in is a standard input stream  
        System.out.print("Enter first number- ");  
        int a = sc.nextInt();  
        System.out.print("Enter second number- ");  
        int b = sc.nextInt();  
        System.out.print("Do you want to multiply, add, divide, or subtract? ");  
        String c = sc.next();  
        switch(c) {
            case "multiply":
                System.out.print(a * b);
                break;
            case "add":
                System.out.print(a + b);
                break;
            default:
                System.out.print("Invalid input!");
        }
    }
}
Erik McKelvey
  • 1,650
  • 1
  • 12
  • 23