-1

I'm trying to create a command/console calculator program but I'm getting stuck with the if statements for the operators.

I know this isn't the easiest (or even the correct) way to create a calculator program but I'm doing it this way so I can practice user Output, Input, If statements and calculations.

I was thinking that for the operator variable it would be type char, but if I do it that way I cant get the user input. Whats wrong with the following code?

package calculator;
import static java.lang.System.in ;
import java.util.Scanner;

public class Calculator {


 public static void main(String[] args) {

     Scanner in = new Scanner(System.in);

 System.out.println ("Welcome to the calculator!");
 System.out.print ("Enter a calculation: ");

 int a;
 int b;
 String Op; //Operator


 a = in.nextInt();
 b = in.nextInt();
 Op = in.next();

 if (Op == '*') { a*b ;}
 if (Op =='/') { a/b ;} 
 if (Op == '+') {a+b;} 
 if (Op == '-') {a-b;} 



}

}

I haven't finished it off yet (as you can see there isn't any output) but that's because I cant get the if statements to work.

The errors I'm getting are:

 error: not a statement
 if (Op == '-') {a-b;} 

This is for every if statement.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Kevin Panko Apr 10 '14 at 13:24

5 Answers5

2

There are few things that you need to consider:

  1. Op is string so you need to compare using .equals("op") or .equalsIgnoreCase("op") method.
  2. You are getting error as not a statement because you need to use ; to terminate the statement so a-b is not statement you need to do like I am using on following code.
  3. And last you are checking with '*' single code uses for char

You can see following code with formatted language.

public class Calculator {

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    System.out.println("Welcome to the calculator!");

    int a;
    int b;
    String Op; //Operator
    int c = 0;

    System.out.println("Enter First Value :");
    a = in.nextInt();
    System.out.println("Enter Second Value :");
    b = in.nextInt();
    System.out.println("Enter Operator :");
    Op = in.next();

    if (Op.equals("*")) {
        c = a * b;
    } else if (Op.equals("/")) {
        c = a / b;
    } else if (Op.equals("+")) {
        c = a + b;
    } else if (Op.equals("-")) {
        c = a - b;
    }

    System.out.println("Output is " + c);

}
}
Yubaraj
  • 3,800
  • 7
  • 39
  • 57
1
 if (Op == '*') { a*b ;}
 if (Op =='/') { a/b ;} 
 if (Op == '+') {a+b;} 
 if (Op == '-') {a-b;} 

You can't compare a String and a char via ==. But exchanging your Char with a String will help you neither. To compare two String objects you have to use string1.equals(string2).

See How do I compare Strings in Java for further information on that.


Unfortunately, in this particular case, it will unnecessary complicate your code. I suggest using a switch-case statement here:

switch (Op) {
    case "*" :
        a *= b;
        break;

    case "/" :
        a /= b;
        break;

    case "+" :
        a += b;
        break;

    case "-" :
        a -= b;
        break;

    default:
        System.out.printf("Invalid operation entered!\n");
}
Community
  • 1
  • 1
ifloop
  • 8,079
  • 2
  • 26
  • 35
  • @sp00m indeed we can have String in switch case ( from java7) – exexzian Apr 10 '14 at 13:00
  • @sp00m Did you even try this? See [The switch statement](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html) for further information. – ifloop Apr 10 '14 at 13:01
0

You are comparing String with char so

change '-' with "-" ( do same for other operators as well ) and compare it as Op.equals("-")

and about error: not a statement;

assign the result to a variable like
result = a-b

exexzian
  • 7,782
  • 6
  • 41
  • 52
0

You can't compare two Strings using ==. == will compare th Objects, not the content of the Strings. To compare Strings use someString.equals(otherString);. Further, it's String s = "someString"; and not not String s = 'someString'.

Try this:

 if (Op.equals("*")) { a*b ;}
 if (Op.equals("/")) { a/b ;} 
 if (Op.equals("+")) {a+b;} 
 if (Op.equals("-")) {a-b;}
MichaelS
  • 5,941
  • 6
  • 31
  • 46
-3

You should perform string comparison this way:

if(op.equals("*")

Since, you are using the == operator for strings you are getting this error.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Rst
  • 23
  • 1
  • 7
  • 1
    Your answer is incorrect. Using `==` over `.equals()` will not result in a compiler error. `error: not a statement` is due to the calculation without storing the result to a variable (`a*b` should be `result = a*b` or something to that effect). – ryvantage Apr 10 '14 at 16:36