-2

This program is supposed to be a basic calculator that takes two double inputs and then prompts for the user to type a string with a corresponding mathematical operator. For some reason my basic calculator will not return a value after the user has input a String corresponding to the menu.

 import java.util.Scanner;  
    public class Assignment3 {  
        public static void main (String[] args)  
     {
        String operator = "";
        double userInputOne;
        double userInputTwo;
        double output = -.00001;
        String comparison = "";


         Scanner scan = new Scanner(System.in);

         System.out.println("Enter the first number of your calculation:");
         userInputOne = scan.nextDouble();

         System.out.println("Enter the second number of your calculation:");
         userInputTwo = scan.nextDouble();

          System.out.println("Type in one direction under Calculator Options:");
          System.out.println("Add, Subtract, Multiply, Divide, Mod");
          System.out.println("Compare");
          System.out.println("Print Numbers");
          System.out.println("Quit");
          Scanner scanTwo = new Scanner(System.in);
          operator = scanTwo.nextLine();

        if (operator == "Add")
        {
            output = userInputOne + userInputTwo;
        }else if (operator == "Subtract"){
            output = userInputOne - userInputTwo;
        }else if (operator == "Multiply"){
            output = userInputOne * userInputTwo;
        }else if (operator == "Divide"){
            output = userInputOne / userInputTwo;
        }else if (operator == "Mod"){
            output = userInputOne % userInputTwo;
        }else if (operator == "Compare"){
            if(userInputOne == userInputTwo){
                comparison = (userInputOne + " Is equal to " +      userInputTwo);
                System.out.println(comparison);
            }else if (userInputOne > userInputTwo){
                comparison = (userInputOne + " Is greater than " + userInputTwo);
                System.out.println(comparison);
            }else if (userInputOne < userInputTwo){
                comparison = (userInputOne + " Is less than " + userInputTwo);
                System.out.println(comparison);
            }
        }else if (operator == "Print Numbers"){
            comparison = userInputOne + "  " + userInputTwo;
            System.out.println(comparison);
        }else if (operator == "Quit"){
            System.exit(0);
        }


        if (output != -.00001){
        System.out.println(output);
        }

}

}
amwindso
  • 3
  • 2

3 Answers3

4

You can't compare strings with == you need to use operator.equals()

For example instead of operator == "Subtract" use "Subtract".equals(operator)

FDinoff
  • 30,689
  • 5
  • 75
  • 96
2

For String value comparisons, you need to use the String#equals() method.

== is used for object reference comparisons.

Hence, all your operator comparisons need to be like this:-

if ("Add".equals(operator))
Rahul
  • 44,383
  • 11
  • 84
  • 103
0

The problem is the use of the == for comparing Strings. Which Java version are you using? As stated by others, you can use equals() instead, but as of of Java 7, you can use switch for String comparison:

switch (operator) {

    case "Add":
        utput = userInputOne + userInputTwo;
        break;

    case "Subtract":
        utput = userInputOne - userInputTwo;
        break;

    case "Multiply":
        utput = userInputOne * userInputTwo;
        break;

    // ...

    default:
       // optional default statement
       break;
} 

Note the break statement at the end of each case to prevent fall through.

More information is available in the Oracle's Java Tutorials (scroll down to "Using Strings in switch Statements").

matsev
  • 32,104
  • 16
  • 121
  • 156