Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calculator Java Input

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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!");
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading