1

My program's not working. What do you think is wrong?

Scanner in = new Scanner(System.in);
    System.out.print("Enter first number: ");
    double num1 = in.nextDouble();
    System.out.print("Enter second number: ");
    double num2 = in.nextDouble();
    System.out.println("Enter operation to perform: ");
    String oper = in.next();

    if(oper == "add" || oper == "addition" || oper == "+") {
        double sum = num1 + num2;
        System.out.printf("The sum of the two numbers is %d", sum);
    }

When I type the operation(which is a String), program terminates. Output:

Enter first number: 12
Enter second number: 8
Enter operation to perform: 
"add"

Process completed.

I can't seem to find the error, please help?

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
Ciara
  • 197
  • 1
  • 3
  • 11
  • 4
    try typing add without " AND do what the others say: Strings and "==" = bad idea! – Fildor Jan 24 '13 at 12:36
  • 1
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – jlordo Jan 24 '13 at 12:40
  • @jlordo with the addition, that he is typing " on the console. `"add"` will never equal `add`. Using equals or == ... – Fildor Jan 24 '13 at 12:41
  • All the answers about == are correct. Thiss is one of the true stupidities of the java language. In an attempt to be "correct" they built a trap for new users. Most other languages make ths wok as expected, ome times introducing === for what Java does with ==. Getting this wrong is not your fault, everybody gets it wrong at first. – Peter Wooster Jan 24 '13 at 12:42

9 Answers9

4

Never compare strings with operator == - it is rough mistake. Use equals instead:

if(oper.equals("add") || oper.equals("addition") || oper.equals("+")) {

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
2

Do not use == use the equals method :

if(oper.equals("add") || oper.equals("addition") || oper.equals("+")) 

== operator is used to compare address in memory space rather than the content of the strings being compared

giorashc
  • 13,691
  • 3
  • 35
  • 71
2

Don't compare Strings using ==. Always use equals():

if("add".equals( oper ) || "addition".equals( oper ) || "+".equals( oper ) ) {

// ...
}

With == you compare object references (or primitive types). Strings are objects in Java, so when you compare oper and add, both point to different objects. Thus even if they contain the same value, the comparison with == fails, because they are still different objects.

Sirko
  • 72,589
  • 19
  • 149
  • 183
1
if(oper == "add" || oper == "addition" || oper == "+") {

should be

if(oper.equals("add") || oper .equals("addition") || oper.equals("+")) {

use .equals method to check if two strings are meaningfully equal, == operator just checks if two reference variables refer to the same instance.

PermGenError
  • 45,977
  • 8
  • 87
  • 106
1

Don't compare Strings using ==. Use equals instead.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
1

Compare strings by using equals(..) not ==

replace

if(oper == "add" || oper == "addition" || oper == "+") {

by

if(oper.equals("add") || oper.equals("addition") || oper.equals("+")) {

== compares for same reference not same content.

MrSmith42
  • 9,961
  • 6
  • 38
  • 49
1

Do what all the others say: use equals or even equalsIgnoreCase. (There are good explanations for this so in the other answers. Would be silly to repeat it here.)

AND type "add" without the " in the console.

Only doing both will work.

Fildor
  • 14,510
  • 4
  • 35
  • 67
0

use this

    if("add".equals(oper)  || "addition".equals(oper) || "+".equals(oper)) {
double sum = num1 + num2;
        System.out.printf("The sum of the two numbers is %d", sum);
    }
NPKR
  • 5,368
  • 4
  • 31
  • 48
0

In addition to using equals() or still better equalsIgnore() instead of == for strings, you also need to enter add in the command-line instead of "add".

Or else, you have to compare it as:

oper.equals("\"add\"")

Also, you seem to be from a C background. Normally in Java, one would use:

System.out.println("The sum of the two numbers is " + sum);

instead of

System.out.printf("The sum of the two numbers is %d", sum);

since %d prints integer value and not double value.

TechSpellBound
  • 2,505
  • 6
  • 25
  • 36