0

I don't know why this is not working I get no compilation error here but the program always returns the else statement. Am I supposed to define operation some other way or call it someway else?

import javax.swing.JOptionPane;

public class Calculator1 {

public static void main(String []Args) {

  String firstNum;
  String operation;
  String secondNum;

  firstNum = JOptionPane.showInputDialog("Input a number.");
  secondNum = JOptionPane.showInputDialog("Input another number.");

  double num1 = Double.parseDouble(firstNum);
  double num2 = Double.parseDouble(secondNum);

  operation = JOptionPane.showInputDialog("Input an operation sign.");

  if (operation == "x") {
    System.out.println( num1 * num2 );
  }
  if (operation == "*") {
    System.out.println( num1 * num2 );
  }
  if (operation == "/") {
    System.out.println( num1 / num2 );
  }
  if (operation == ":") {
    System.out.println( num1 / num2 );
  }
  if (operation == "+") {
    System.out.println( num1 + num2 );
  }
  if (operation == "-") {
    System.out.println( num1 - num2 );
  }
  else {
    System.out.println("Please enter an appropriate operation sign.");
  }

} }

OFA
  • 13
  • 2

3 Answers3

0

The problem is with your if statements. The else statement will always execute if operation is not equal to "-". This is because each of your if statements is a separate block of code.

if(x) {}

if(y) {}

if(z) {}
else {}

You can make this work by using else if instead of several if statements.

if(x) {}
else if(y) {}
else if(z) {}
else {}

This will work, but the correct way to do this would be to use a switch statement.

switch(operation) {
    case "x": result = num1 * num2 ;
    break;
    case "/": result = num1 / num2;
    break;
    case "-": result = num1 - num2;
    break,
    default: System.out.println(errorMessage);
}
Edwin Owen
  • 32
  • 4
-1

You should use "x".equals(operation);

-1

First, you need use if/else structure:

  if (operation == "x") {
    System.out.println( num1 * num2 );
  }
  else if (operation == "*") {
    System.out.println( num1 * num2 );
  }
  else if (operation == "/") {
    System.out.println( num1 / num2 );
  }
  // Continue...

Next thing, in java, you can't compare string's content with '==' operator. You should use equals method:

 if (operation.equals("x")) {
    System.out.println( num1 * num2 );
  }
  else if (operation.equals("*")) {
    System.out.println( num1 * num2 );
  }
  else if (operation.equals("/")) {
    System.out.println( num1 / num2 );
  }
  // Continue...

This should works, but. Why doesn't works the '==' operator?

Strings are objects in java, you handle objects with references. So when you are doing '==', you are comparing references's address. If you want compare the content, you have to use equals method.

Another option will be use a switch:

switch(operation)
{
    case "+":
            System.out.println(num1 + num2);
            break;
    case "-":
            System.out.println(num1 - num2);
            break;
    case "/":
            System.out.println(num1 / num2);
            break;
    case "x":
    case "*":
              System.out.println( num1 * num2 );
              break;
    default:
              System.out.println("Error!");
}
amchacon
  • 1,891
  • 1
  • 18
  • 29