2

I'm new to java programming. I tried to make calculator that can do 4 basic math operations using if statement. However I don't have it working as expected. When trying to parse operator, it just finishes with else statement.

I guess I have not properly formatted if statement ?

Any help is greatly appreciated.

Thanks.

import java.util.Scanner;
import java.lang.Object;

public class calc {
    public static void main(String args[]) {
        System.out.println("Test kalkulator za  sabiranje");

    Scanner keyboard = new Scanner(System.in);
    double fnum, snum, res;
    String ch = "";


    System.out.println("Enter first number: ");
    fnum = keyboard.nextDouble();

    System.out.println("Enter operation: ");
    ch = keyboard.next();

    if( ch == "+") {

        System.out.println("Enter second number: ");
        snum = keyboard.nextDouble();
        res = fnum + snum;
        System.out.println("Result is: "+ res);

    }

    else if ( ch == "-") {

        System.out.println("Enter second number: ");
        snum = keyboard.nextDouble();
        res = fnum - snum;
        System.out.println("Result is: "+ res);
    }

    else if ( ch == "/") {

        System.out.println("Enter second number: ");
        snum = keyboard.nextDouble();
        res = fnum / snum;
        System.out.println("Result is: "+ res);
    }

    else if( ch == "*") {

        System.out.println("Enter second number: ");
        snum = keyboard.nextDouble();
        res = fnum * snum;
        System.out.println("Result is: "+ res);
    }
    else {
        System.out.println("You entered wrong operator, please try again");
    }
    keyboard.close();

}
}
Perp
  • 364
  • 7
  • 15
  • 1
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – guido Oct 24 '15 at 21:03
  • You're not comparing your strings properly. Since you're storing the operator in a String, you need to use `.equals()` instead of `==`. Example: `if (ch.equals("+"))` as opposed to `if (ch == "+")` – wadda_wadda Oct 24 '15 at 21:04

3 Answers3

3

String objects are reference objects, meaning when you type out code like

str == "+"

you're checking to see if the point in memory where str is located is equal to +. To verify if two strings equate each other, you need to use the method .equals like so

str.equals("+")
Matthew Chrobak
  • 384
  • 2
  • 8
  • Thanks, it's working now. But could I use char instead of string and compare char inside if statement ? – Perp Oct 24 '15 at 21:29
  • If you're comparing a string with another string, you need to use the .equals method. If you're comparing two chars, you can use the == comparator. From this, I feel like it's important to state that you can always check if two variables have equal value using == IF they are primitive data types. Anything else needs to be compared through more complicated methods. – Matthew Chrobak Oct 24 '15 at 21:32
1

instead of == you should use equals method

Siva Kumar
  • 1,983
  • 3
  • 14
  • 26
1

Your code is fine, the problem is when you compare ch with the strings "+","-", and so on...
In java strings are Objects. Comparing objects with the == operator would only return true if the objects referrring to the same object. In order to actually compare the two objects you need to use the equals() method.
So to sum up, the correct conditions should be:
if(ch.equals("+")){ } for every comparison.

Gershon Papi
  • 5,008
  • 3
  • 24
  • 50