PDA

View Full Version : Can someone help fix my Java errors?




apattee
Nov 5, 2007, 10:51 PM
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
Nov 5, 2007, 10:56 PM
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
Nov 5, 2007, 11:00 PM
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
Nov 5, 2007, 11:17 PM
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


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:


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

apattee
Nov 5, 2007, 11:35 PM
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


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:


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



thank you! you made life a lot easier for me!