11

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 " );
    }
}
nIcE cOw
  • 24,238
  • 7
  • 47
  • 133
Tanner Banks
  • 201
  • 1
  • 2
  • 4

5 Answers5

104

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.

Jon Skeet
  • 1,335,956
  • 823
  • 8,931
  • 9,049
4

As far as I know, you can't use the & operator in Java to perform a bitwise comparison between doubles. It can only be used with other simpler primitives, like ints and chars.

In addition, the way you're using the & operator will not perform a bitwise comparison between the numbers because you're using it to compare the results of P>Q and R<S, both of which produce boolean values.

To perform a bitwise comparison between doubles, you need to use a different technique to directly compare P with Q and R with S. Here's an example of one way to do that: https://stackoverflow.com/a/13928322/213343.

Community
  • 1
  • 1
shovavnik
  • 2,838
  • 3
  • 22
  • 21
  • Where is he trying to perform bitwise comparison between doubles? – matehat Sep 27 '13 at 16:21
  • Not in the code. His question start with: "I'm trying to run a Bitwise number comparison...". My answer explains why his code is not accomplishing that goal. Plus, this fits with the incorrect usage of the & operator, which *is* the bitwise AND operator. – shovavnik Sep 27 '13 at 16:27
2
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 " );
}

and the Effective Way is

    if (First_Relation && Second_Relation)
    {
        System.out.println("Given the values for p,q,r, and s the expression "
        + "(p > q) && !(r < s) evaluates to " );
    }
Ashok
  • 1,241
  • 11
  • 18
1

Try to take

(First_Relation = true) & (Second_Relation = true)

into brackets. And remove the ";" from the end of "if" statement, cause it makes no sense : ";" is considered as a new statement termination (empty statement in your case) and as you didnt provide the scope for "if" statement - it works only for the next statemnt i.e. empty statement.

1

If the condition is not met then there's no message. I therefore suggest:

boolean evaluation = (P > Q) && !(R < S);
System.out.println("Given the values for p,q,r, and s the expression "
    + "(p > q) && !(r < s) evaluates to " + evaluation);
Lluis Martinez
  • 1,916
  • 7
  • 28
  • 41