Calculator problems

I'm working on a VERY simple calculator with Java and doesn't seem to give an answer after selecting an operation. The code looks like this:

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 result:

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 much appreciated. Postscript no errors. Thank!

+3


source to share


4 answers


There are 2 problems. Problem with Scanner and condition

keyboard.nextDouble()

, keyboard.next()

etc. (all except .nextLine()

) leave a new line ( \n

or Enter

) in the buffer . This can cause problems, etc.

I suggest adding a separator using keyboard.useDelimiter("\n");

. You only need to do this once and do it right after initialization keyboard

.

Thus, he will only see Enter

as a signal to end this current input.



In addition, all conditions must use the .equals()

or method .equalsIgnoreCase()

, which is written as:

operation.equals("Multiplication");

      

or

operation.equalsIgnoreCase("multiplication");

      

+3


source


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



0


source


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

! Instead !

0


source


As @Sebastian said, you should use equals instead of == You can improve it a bit to avoid space or case errors (uppercase or lowercase) by doing it like this:

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

      

So it will work for multiplication multiplication, but also MulTIpliCation, etc.

You can get the same result using equalsIgnoreCase also

0


source







All Articles