javamacoscompilationosx-tiger

javac says int (x = 1); is not a statement then says ';' expected. What's wrong with my code?


I'm on a Mac Mini G4 trying to learn Java. When I try to compile "DooBee.java" by typing "javac DooBee.java" at the terminal I get two errors. This is what my terminal looks like:

> nephi-shields-mac-mini:/developer/MyProjects
> nephishields$ javac DooBee.java
> DooBee.java:5: not a statement
>                 int (x = 1);
>                 ^ DooBee.java:5: ';' expected
>                 int (x = 1);
>                     ^ 2 errors nephi-shields-mac-mini:/developer/MyProjects
> nephishields$

This is what I have typed into my "DooBee.java" file:

public class DooBee {
    public static void main (String[] args) {
        int (x = 1);

        while (x < 3) {
            System.out.print ("Doo");
            System.out.print ("Bee");
            x = x + 1;
        }

        if (x == 3) {
           System.out.print ("Do");
        }
    }
}

Have I made a mistake? Or is there something wrong with my computer? sorry if this question (or a similar one) has already been asked. I honestly tried to find an answer on my own (google searches, searching Stack Overflow, rewrote my code several times, checked my book "Head First Java" to make sure I was typing things the right way) but came up empty.


Solution

  • The problem is that (x = 1) is an expression, not a declaration, so it can't be used to declare the variable x. Remove the parentheses and you'll have a correct declaration with initializer.