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 " );
    }
}
Jon Skeet
people
quotationmark

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.

people

See more on this question at Stackoverflow