1

Been trying to make a simple calculator add, subtract, divide, multiply. But it is not allowing me to get to the calculation part. It always says "incorrect type of calculation" even when I type +,-,/, or '' How do I get the scanner to understand the +,-,/,'' as input? Thanks

import java.util.Scanner;

public class Calculator {

    public void typeOfCalc(){
        Scanner user_input = new Scanner(System.in);
        System.out.print("What type of calculation do you want? \n Addition? type '+' \n Subtraction? type '-' \n Division? type '/' \n or Multiplication type '*' \n");
        String calcType = user_input.next().trim();
       if (calcType != "+" || calcType != "*" || calcType != "/" || calcType != "-"){
           System.out.println("Incorrect type of calculation, try again \n");
           typeOfCalc();
       }

        else if (calcType == "+"){

            System.out.print("Chose your first number \n");
            int num1 = user_input.nextInt();
            System.out.print("Chose your second number \n");
            int num2 = user_input.nextInt();
            System.out.print(num1 + " + " + num2 + " = \n" + (num1 + num2) + "\n");
        }
        else if (calcType == "-"){

            System.out.print("Chose your first number \n");
            int num1 = user_input.nextInt();
            System.out.print("Chose your second number \n");
            int num2 = user_input.nextInt();
            System.out.print(num1 + " - " + num2 + " = \n" + (num1 - num2) + "\n");
        }
        else if (calcType == "/"){

            System.out.print("Chose your first number \n");
            int num1 = user_input.nextInt();
            System.out.print("Chose your second number \n");
            int num2 = user_input.nextInt();
            System.out.print(num1 + " / " + num2 + " = \n" + (num1 / num2) + "\n");
        }
        else if (calcType == "*"){

            System.out.print("Chose your first number \n");
            int num1 = user_input.nextInt();
            System.out.print("Chose your second number \n");
            int num2 = user_input.nextInt();
            System.out.print(num1 + " * " + num2 + " = \n" + (num1 * num2) + "\n");
        }
    }

}
pewpew
  • 700
  • 2
  • 9
  • 32

2 Answers2

4

You need to use .equals() to check String equality.

So calcType.equals("*") for equality or !calcType.equals("*") to check that they are not the same.

Strings are immutable, so when you use == it is checking whether it is the same String in memory, not checking the actual contents of the String

user1231232141214124
  • 1,339
  • 3
  • 16
  • 22
3

Once adjusting your code to follow @redFIVE's advice (Replacing != with !myVar.equals(str)) make sure to replace your ORs (||) with ANDs (&&).

Consider the following:

if (!calcType.equals("+") || !calcType.equals("*") || ...)

If the input is "+" the condition will still pass, because calcType doesn't equal "*", and the logic evaluates to TRUE.

When you use an AND: if (!calcType.equals("+") && !calcType.equals("*") && ...) you should see the expected results.


However, the main issue with your current code is due to your misuse of != to compare Strings. Fix that, and you can at least use a debugger to see why the above logic could fail, or produce unexpected results.

CubeJockey
  • 2,209
  • 8
  • 24
  • 31
  • cool, and so I also need to make the "else if " statements the same way. calcType.equals("+"). Thanks! – pewpew Dec 11 '15 at 19:08