-2

This program does the following: First input obtains two numbers from the user, second input if the user inputs (+) symbol the program adds the two numbers together, if the user enters (-) symbol it subtracts the 1st number from the 2nd number. But no results are shown the program just runs and terminates.

import javax.swing.JOptionPane;

public class ObtainNumber {
public static void main(String []args) {

    String strNum1 = JOptionPane.showInputDialog("Enter the first number");
    String strNum2 = JOptionPane.showInputDialog("Enter the second number");

    int num1 = Integer.parseInt(strNum1);
    int num2 = Integer.parseInt(strNum2);

    String askForOperation = JOptionPane.showInputDialog("What operation needs to be done?");

    int sum;
    if (askForOperation == "+") {
            sum = num1 + num2;
            JOptionPane.showMessageDialog(null, "The result of the Addition is " + sum);
    }

    double subtract;
    if (askForOperation == "-") {
            subtract = num2 - num1;
            JOptionPane.showMessageDialog(null, "The result of the subtraction is " + subtract);

    }



    }

}

Django
  • 135
  • 1
  • 2
  • 16

6 Answers6

6
if (askForOperation == "+")  // Reference is not same

Do the comparition check using equals method.

if ("+".equals(askForOperation))  // Compare content
Kick
  • 4,823
  • 3
  • 22
  • 29
  • While this is true, can you explain it little bit more? What is wrong with `==`? – Pshemo Feb 25 '14 at 14:16
  • 1
    == only check the reference whereas equals method compare the value. – Kick Feb 25 '14 at 14:19
  • I know that. Point in answering is not only to give solution, but also explain problem. Don't give fish, give rod and teach how to use it. Let OP learn *why* `==` is wrong here. Put your comment in your answer. – Pshemo Feb 25 '14 at 14:20
  • Sry cant understand @Pshemo the reference of askForOperation and + is not same – Kick Feb 25 '14 at 14:22
  • @fge i also say that reference is not same so need to use equals to compare the content – Kick Feb 25 '14 at 14:25
1

Try this

if (askForOperation.equals("+")) {
        sum = num1 + num2;
        JOptionPane.showMessageDialog(null,
                   "The result of the Addition is " + sum);
}

To avoid any Nullpointer exception, in any case

do this,

final String plus = "+";
if (plus.equals(askForOperation)) {
        sum = num1 + num2;
        JOptionPane.showMessageDialog(null,
                   "The result of the Addition is " + sum);
}
Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56
1

This test:

askForOperation == "+"

is wrong. What is does is compare the references of askForOperation and (unnamed) string "+", which is not the same at all.

You want to compare the contents; therefore you must use:

"+".equals(askForOperation)

Same for "-".

Sample:

String h1 = "hello";
String h2 = new String("hello");
h1 == h1; // true: same reference
h1 == h2; // false: different reference
h1.equals(h2); // true: same contents

Note that strings are a little special due to string literals -- "+" will "silently" create a String object for you, which is why you can write "strange-looking" code such as "foo".equals(bar)

fge
  • 119,121
  • 33
  • 254
  • 329
  • @Pshemo gee, I keep forgetting about this one :/ – fge Feb 25 '14 at 14:26
  • That (because it works for interned literals) is probably main reason why people are trying to use `==` to compare all Strings :/ – Pshemo Feb 25 '14 at 14:28
0
if(askForOperation.equals("+"){
    do smth..
}

Use of == for String is wrong,you must compare content like String1.equals(String2);

Kick
  • 4,823
  • 3
  • 22
  • 29
Ruben JG
  • 97
  • 7
0

try

    .equals

method insteed of ==

== compares the object reference and .equals compares value of the object value.

saravanakumar
  • 1,747
  • 4
  • 20
  • 38
0

I think this will clear your doubt.

http://www.javabeat.net/what-is-difference-between-equals-and/

user3213851
  • 1,068
  • 2
  • 12
  • 24