Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

apattee

macrumors regular
Original poster
Oct 19, 2006
167
4
I can't figure out what's wrong with it even after plugging it into JGrasp. Thanks for any help.

public class Mystery
{
static final int ONE = 5;

public static void main(String[] args)
{

int x, y, w, z;
z = 9;

if (z > 10)
x = 12, y = 5, w = x + y + one;
else
x = 12, y = 4, w = x + y + one;

System.out.println("w = " + w);
}
}
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Without some sort of hint as to the error you are receiving whilst compiling or the behaviour you are getting but do not expect neither can we.
 

apattee

macrumors regular
Original poster
Oct 19, 2006
167
4
Mystery.java:12: ';' expected
x = 12, y = 5, w = x + y + one;
^
Mystery.java:12: ';' expected
x = 12, y = 5, w = x + y + one;
^
Mystery.java:13: 'else' without 'if'
else
^
Mystery.java:14: <identifier> expected
x = 12, y = 4, w = x + y + one;
^
Mystery.java:16: <identifier> expected
System.out.println("w = " + w);
^
Mystery.java:16: illegal start of type
System.out.println("w = " + w);
^
Mystery.java:18: class, interface, or enum expected
}
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Well, the first error is that on line 12 the compiler expects as ; to terminate the statement. You appear to be trying to chain a lot of statements on a single row using ,. This is simply not allowed. You need to put a ; at the end of each statement like

Code:
x=12; y=5; w=x+y+one;

Of course this will result in each being a separate statement so only the first will be conditional on the if statement. Assuming you want all 3 statements to be conditional you want to wrap them in the statement compounding operator {} like so:

Code:
if (z>10)
{
x=12;
y=5;
w=x+y+one;
}
 

apattee

macrumors regular
Original poster
Oct 19, 2006
167
4
Well, the first error is that on line 12 the compiler expects as ; to terminate the statement. You appear to be trying to chain a lot of statements on a single row using ,. This is simply not allowed. You need to put a ; at the end of each statement like

Code:
x=12; y=5; w=x+y+one;

Of course this will result in each being a separate statement so only the first will be conditional on the if statement. Assuming you want all 3 statements to be conditional you want to wrap them in the statement compounding operator {} like so:

Code:
if (z>10)
{
x=12;
y=5;
w=x+y+one;
}


thank you! you made life a lot easier for me!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.