-1

Ok so I'm teaching myself (along with the help of some youtube videos) java. So after a few videos I thought I would try and test my skills by setting up a calculator. now I had done this before with lua but its a somewhat diffrent ball game here. my issue is that no matter how i set up any variables or if/else statements, the program always ends up just skipping over the else ifs. i put a final else at the base of the code to see if it was even reaching the bottom and it was definitly, so heres my code

import java.util.Scanner;


public class calculator {
public static void main(String[] args){
    double Answer;
    String op;
    double num1;
    double num2;

    Scanner input = new Scanner(System.in);

    System.out.print("What opperation would you like to preform? +,-,*,/, :");
    op = input.nextLine();


    System.out.print("What is the first number? : ");
    num1 = input.nextDouble();

    System.out.print("And the seccond number? : ");
    num2 = input.nextDouble();


    if(op == "+"){
        Answer = (num1 + num2);
        System.out.print(Answer);

    }else if(op == "-"){
        Answer = num1 - num2;
        System.out.print(Answer);

    }else if(op == "*"){
        Answer = num1 * num2;
        System.out.print(Answer);

    }else if(op == "/"){
        Answer = num1 / num2;
        System.out.print(Answer);

    }else{
        System.out.print("ilikecake");
    }

}

}

as you can see, i used a string for the opperation (op) variable due to the flexability of strings in this particular application, also, i couldn't figure out how to get scanner to accept a char variable. i keep getting the string can not be converted to char error so i just set it up as a string. if you can help thank you! also please ignore any akward formatting from the post, i couldn't figure out how to make the code appear properly so i did my best :/

user2620255
  • 137
  • 1
  • 6
  • Not a java programmer, but check this out: http://stackoverflow.com/questions/767372/java-string-equals-versus – Gray Jul 25 '13 at 20:00
  • 1
    Compare String values with String's `equals` method, which compares string contents, not with `==`, which compares two objects to see if they are the same object. – rgettman Jul 25 '13 at 20:00
  • 1
    Also use lowercase variable names in Java (Answer -> answer). Capitalized names are used only for classes and interfaces. – Ma3x Jul 25 '13 at 20:03

2 Answers2

5

Use the

.equals()

method for comparing strings. The == operator tests for equal references, and you want to check for equivalent values.

Read more here: How do I compare strings in Java?

Community
  • 1
  • 1
Undefined
  • 655
  • 1
  • 4
  • 13
2

Do NOT compare Strings with ==. To compare Strings, use the .equals() method., aka: if (op.equals("+"))

In short, == compares references, whereas .equals() actually compares the the contents (values) of the Strings. I would go into more detail, but it has been covered in many threads, for example.

Community
  • 1
  • 1
Steve P.
  • 14,489
  • 8
  • 42
  • 72