ripsly25
ripsly25

Reputation: 11

Java Calculator Switch Issue

I incorporated a couple different methods I've seen on here. Does anyone know how to fix this problem I am having? When you use this code, It asks for you to enter the mathematical operator, BUT when I do if I enter +9 or -%, it will still work and use the first symbol. I want it to give an error if someone inputs */ instead of just *. I even tried switching the case to numbers (ie case 1:) and it will do the same thing if I set addition to 1 and if I enter 15, it will read the one and do addition. Any ideas?

import java.util.Scanner;

public class javacalculator {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Scanner operation = new Scanner(System.in);

        double num1, num2, answer;
        char userOperation;
    

        System.out.print("Enter your full name: ");
        String userName = input.nextLine();

        System.out.println("+ Addition");
        System.out.println("- Subtraction");
        System.out.println("* Multiplication");
        System.out.println("/ Division");
        System.out.println("% Modulus");
        System.out.println("\n");


        System.out.print("Enter mathematical operator e.g. + for Addition: ");
        userOperation = operation.next().charAt(0);

        boolean invalidOperator = false;

        switch (userOperation) {
        case '+':
            System.out.print("Enter first number: ");
            num1 = input.nextInt();

            System.out.print("Enter the second number: ");
            num2 = input.nextInt();

            answer = num1 + num2;

            System.out.print(num1 + " + " + num2 + " = " + answer);


            break;
        case '-':
            System.out.print("Enter first number: ");
            num1 = input.nextInt();

            System.out.print("Enter the second number: ");
            num2 = input.nextInt();

            answer = num1 - num2;

            System.out.print(num1 + " - " + num2 + " = " + answer);

            break;
        case '*':
            System.out.print("Enter first number: ");
            num1 = input.nextInt();

            System.out.print("Enter the second number: ");
            num2 = input.nextInt();

            answer = num1 * num2;

            System.out.print(num1 + " * " + num2 + " = " + answer);


            break;
        case '/':
            System.out.print("Enter first number: ");
            num1 = input.nextInt();

            System.out.print("Enter the second number: ");
            num2 = input.nextInt();

            answer = num1 / num2;

            System.out.print(num1 + " / " + num2 + " = " + answer);


            break;

        case '%':
            System.out.print("Enter first number: ");
            num1 = input.nextInt();

            System.out.print("Enter the second number: ");
            num2 = input.nextInt();

            answer = num1 % num2;

            System.out.print(num1 + " % " + num2 + " = " + answer);


            break;
        default:
            System.out.println("Invalid operator!");
            break;
    }


}

}

Upvotes: 0

Views: 71

Answers (1)

Guneet Singh
Guneet Singh

Reputation: 11

Let us break this down: operation.next().charAt(0);

operation.next() -> Gives you full input string

.charAt(0); -> returns the first character of the full input string

So anything we enter +9 or */ -> it returns the first element.

Now let's handle this case:

String o = operation.next(); // here is complete input
if(o.length()!=1){
userOperation = 0;     //if length of input is not 1 we set custom value
}else{
userOperation = o.charAt(0); //get and set the first and only element 
}

Hopefully it helps!

Upvotes: 1

Related Questions