Java - Input and If statements

Asked by At

I'm new to programming with Java so please bare with me. 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. Any help will be greatly appreciated.

5 Answers

0
exexzian On

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

-3
Rst On

You should perform string comparison this way:

if(op.equals("*")

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

0
MichaelS On

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;}
1
ifloop On
 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");
}
2
Yubaraj On

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);

}
}