3

I am working on a VERY simple calculator with Java and it does not seem to output an answer after selecting the operation. The code is as follows:

import java.util.Scanner;
public class Calc {
public static void main(String[] args) {
    Scanner keyboard = new Scanner (System.in);
    Double fnum;
    Double snum; 
    Double answer;
    String operation; 
    System.out.println("Enter a number.");
    fnum = keyboard.nextDouble();
    System.out.println("Enter another number.");
    snum = keyboard.nextDouble();
    System.out.println("What operation do you wish for? [Multiplication, Addition, Subtraction, Division]");
    operation = keyboard.next(); 
    if (operation == "Multiplication" || operation == "multiplication" || operation == "*") { 
        answer = fnum * snum;
        System.out.print("Your answer is: " + answer);

    }   
    else if (operation == "Division" || operation == "division" || operation == "/") { 
        answer = fnum / snum;
        System.out.print("Your answer is: " + answer);

    }   
    else if (operation == "Addition" || operation == "addition" || operation == "+" || operation == "add") { 
        answer = fnum + snum;
        System.out.print("Your answer is: " + answer);

    }   
    else if (operation == "Subtraction" || operation == "subraction" || operation == "-" || operation == "subtract"){ 
        answer = fnum - snum;
        System.out.print("Your answer is: " + answer);

    }
      else {
        System.out.println("This is not valid.");
    }


}

}

and this is the output:

Enter a number.
6
Enter another number.
6
What operation do you wish for? [Multiplication, Addition, Subtraction, Division]
Multiplication 
This is not valid.

Any help would be very appreciated. P.S. there are no errors. Thanks!

DarthTK421
  • 31
  • 1
  • Darth, replace things like `operation == "Multiplication"` with `operation.compareTo("Multiplication")==0` and it will work. – DWright Apr 26 '15 at 22:32

4 Answers4

3

There are 2 problems. A problem with Scanner and with the condition

keyboard.nextDouble(), keyboard.next() etc... (all except .nextLine() ) leave the newline ( \n or Enter ) in the buffer. This can cause problems etc..

I suggest adding a delimiter using keyboard.useDelimiter("\n");. This only needs to be done once and can be done right after initialization of keyboard.

That way, it will only see the Enter as a signal to end that current input.

Also, the conditions must all be using the .equals() method or .equalsIgnoreCase() which is written as:

operation.equals("Multiplication");

or

operation.equalsIgnoreCase("multiplication");
0

Strings should be compared via .eqauals() method. So put in the if clause operation.equals("Multiplication")...and so on...

Andrej Tihonov
  • 649
  • 2
  • 9
  • 21
0

You are comparing strings by reference, not by their values. Use .equals() instead!

Kristof
  • 249
  • 2
  • 7
0

As @Sebastian said you should use equals instead of == You can improve it a little bit to avoid the spaces mistakes or case (upper case or lower case) issues by doing it the following way:

if (operation.trim().toUpperCase().equals("MULTIPLICATION") || operation.trim().equals("*"))

That way it will work for Multiplication multiplication but also MulTIpliCation, etc.

You can get the same result using equalsIgnoreCase also

alainlompo
  • 4,414
  • 4
  • 32
  • 41