Can someone help fix my Java errors?

Discussion in 'Mac Programming' started by apattee, Nov 5, 2007.

  1. macrumors regular

    Joined:
    Oct 19, 2006
    #1
    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);
    }
    }
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. thread starter macrumors regular

    Joined:
    Oct 19, 2006
    #3
    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
    }
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #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;
    }
    
     
  5. thread starter macrumors regular

    Joined:
    Oct 19, 2006
    #5

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

Share This Page