1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Can someone help fix my Java errors?

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

  1. macrumors regular

    #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

    robbieduncan

    Staff Member

    #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. macrumors regular

    #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

    robbieduncan

    Staff Member

    #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. macrumors regular

    #5

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

Share This Page