Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Illegal Start of Expression Java Boolean?

I'm trying to run a Bitwise number comparison and my code keeps coming up with an Illegal start of expression on line 30 of my code with the "if" statement.

My code reads as so:

public class Project7 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        double P = keyboard.nextDouble(); 
        double Q = keyboard.nextDouble();
        double R = keyboard.nextDouble();
        double S = keyboard.nextDouble();
        boolean First_Relation;
        boolean Second_Relation;

        if (P > Q) First_Relation = true;
        if (R < S) Second_Relation = true;

        if (First_Relation = true) & (Second_Relation = true); 
        System.out.println("Given the values for p,q,r, and s the expression "
        + "(p > q) && !(r < s) evaluates to " );
    }
}
like image 447
Tanner Banks Avatar asked Sep 27 '13 13:09

Tanner Banks


People also ask

How do you fix illegal start of expression in Java?

Access modifiers on local variables Therefore, access modifiers have no use here and, if introduced, will raise the “illegal start of expression” error (Fig. 2(a)). Removing the access modifier (as shown on line 5 in Fig. 2(b)) resolves the Java error.

Does Java file throws an error if end with a semicolon?

This error means that you forgot a semicolon in your code. If you look at the full error statement, it will tell you where in your code you should check within a few lines. Remember that all statements in Java must end in a semicolon and elements of Java like loops and conditionals are not considered statements.

What is not a statement error in Java?

The above statement will give a “Error: not a statement” compilation error because its actually not a proper statement. The corrected statement is – Corrected: if(i==1) System.out.println("one"); Example 2: Incorrect: System.out.println("The "+"value of ";+i+" is "+i+" and j is"+j);//i.j being integers.

How do I fix error else without if?

Solution: The above compilation error can be resolved by providing correct if-elseif-else syntax as shown below. The best way to deal with error: 'else' without 'if' is always to use curly braces to show what is executed after if. Learn to indent the code too as it helps in reading and understanding.


1 Answers

An if statement is of the form:

if (condition) statement

You've currently got two bracketed conditions... which also end up assigning values, which probably isn't what you want.

So first fix to get it to compile:

if ((First_Relation = true) & (Second_Relation = true))

Then change the assignments to equality checks, as otherwise it will simply assign true to both variables and the condition will pass regardless of their previous values:

if ((First_Relation == true) & (Second_Relation == true))

Then remove comparisons with boolean constants:

if ((First_Relation) & (Second_Relation))

Then remove unnecessary brackets:

if (First_Relation & Second_Relation)

Then make the variables follow Java naming conventions:

if (firstRelation & secondRelation)

Then use the more conventional && instead of & - && is short-circuiting, and is almost always what you want:

if (firstRelation && secondRelation)

Now you've still got a semi-colon directly after your if condition, which makes it pointless - it will always execute the System.out.println statement, because that's not part of the if statement. You could just remove the semi-colon, but I'd add braces for clarity:

if (firstRelation && secondRelation) {
    System.out.println("insert text here");
}

Next, note that you're only actually initializing your variables if the condition is true - so you'll currently get a compile-time error for trying to read variables which aren't definitely assigned.

First, fix the definite assignment:

// Names changed to follow conventions
boolean firstRelation = p > q;
boolean secondRelation = r < s;

... and the code above should be fine.

Next, spot that you're really gaining very little indeed from those extra variables. Inline the conditions instead:

if (p > q && r < s) {
    System.out.println("Given the values for p,q,r, and s the expression "
    + "(p > q) && !(r < s) evaluates to ";
}

At this point, it becomes very clear that there's a further bug - because your message talks about !(r < s) but the condition is just r < s. So you need to decide what you want to achieve, and make the code and the message reflect the same thing. Note that you don't finish the message, either. Indeed, you could simplify the whole thing to:

System.out.println("Given the values for p,q,r, and s the expression "
    + "(p > q) && !(r < s) evaluates to " + ((p > q) && !(r < s));

... or whatever you want the expression to actually be.

like image 197
Jon Skeet Avatar answered Nov 07 '22 14:11

Jon Skeet