Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to java and trying to build a calculator as the instructor gave me the project.
I am getting the illegal start of expression error in else if statement.
below is my code please help me to find my fault.




Java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package calculators;

import java.util.Scanner;

/**
 *
 * @author barnia data entry
 */
public class Calculators {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner scanner1 = new Scanner(System.in);
        System.out.println("enter 1st number");
        int a = scanner1.nextInt();
        Scanner scanner2 = new Scanner(System.in);
        System.out.println("enter 2nd number");
        int b = scanner2.nextInt();
        Scanner operation = new Scanner(System.in);
        System.out.println("enter operation sign");
        String op = operation.next();
        if (op == -){
            System.out.println(a-b);
        }
        else if (op = +){
        
        System.out.println(a+b);
        }
        else if (op == *){
            System.out.println(a*b);
        }
        else if (op == /){
            System.out.println(a/b);
        }
        else {
            System.out.println(" error in operation");
        }
        
            
        
    }
    
}


What I have tried:

I am trying to build a simple calculator.
Posted
Updated 1-May-18 17:02pm
v2

Based on what posted here, you need to update the code to use .equals() tests for value equality and the character (-+*/) are string type, you need to update the code to wrap it with double quote. See below.

How do I compare strings in Java? - Stack Overflow[^]

Java
public static void main(String args[]) {
        Scanner scanner1 = new Scanner(System.in);
        System.out.println("enter 1st number");
        int a = scanner1.nextInt();
        Scanner scanner2 = new Scanner(System.in);
        System.out.println("enter 2nd number");
        int b = scanner2.nextInt();
        Scanner operation = new Scanner(System.in);
        System.out.println("enter operation sign");
        String op = operation.next();
        if (op.equals("-") ){
            System.out.println(a-b);
        }
        else if (op.equals("+")){
        
        System.out.println(a+b);
        }
        else if (op.equals("*")){
            System.out.println(a*b);
        }
        else if (op.equals("/")){
            System.out.println(a/b);
        }
        else {
            System.out.println(" error in operation");
        }
    }
 
Share this answer
 
You need to wrap each of the values you are comparing op to in double-quotes:

For example:
Java
if (op == -)

has to be:
Java
if (op == "-")


That tells the compiler that you are comparing the op string variable to the double-quoted string constant.

With your code, the compiler thinks you are attempting to subtract something from something else -- thus the "illegal start of expression" as in a subtraction expression.

You'll need to fix all of those comparison statements.

And to compare strings you should not use the == in Java.
Instead you should use the equals method of the string object.

Each of your statements should look like the following (obviously replacing the character you are attempting to check for):

Java
if (op.equals("-"))


The Importance of Examining Error Messages
Also as you're learning to code it is really important to examine the errors you get very closely and then when you are looking for help to reproduce those exact errors when explaining them to others who may be of help.

Here's what I mean. I reproduced your error and let's look closely at what the error looks like:

C:\dev\tools\JavaProjects>..\JavaJDK17\bin\javac first.java
first.java:4: error: illegal start of expression
        if (op == -){
                   ^
1 error


the first line is where I ran the javac (java compiler).
The second line gives you the exact line where the error occurred in the source file (first.java) line 4 -- see the :4? That's what that means.
Also notice there is a caret ^ pointing up at the location where the compiler thinks the problem occurred. We know it is near there so it does begin to help you guess.

Including that kind of error information makes it far easier for someone else to help you find the problem.
 
Share this answer
 
v2
Hello!!
you get that error because the validations of the operations are wrong:
  if (op == -) {
             System.out.println (a-b);
         }
         else if (op = +) {
        
         System.out.println (a + b);
         }
         else if (op == *) {
             System.out.println (a * b);
         }
         else if (op == /) {
             System.out.println (a / b);
         }

The correct form should be:
  if (op == "-") {
             System.out.println (a-b);
         }
         else if (op = "+") {
        
         System.out.println (a + b);
         }
         else if (op == "*") {
             System.out.println (a * b);
         }
         else if (op == /) {
             System.out.println (a / b);
         }

Also, I suggest that instead of using if, you use a switch to avoid the accumulation of nested ifs and else ifs.

regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900