0

I'm attempting a shape-making Java project, and I keep getting a syntax error on my while loop that I can't figure out - any suggestions?

Here's my code thus far:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner scan = new Scanner(System.in);

    System.out.println("Hello! I am a program that draws shapes, but YOU must tell me how big you wish me to make them!");
    System.out.println("Enter shape size: ");
    final int MAX_ROWS = scan.nextInt();

    // DIAMOND
    int spaces = 0, stars, row;

    for (row = 1; row <= (MAX_ROWS + 1) / 2; row++) {
        for (spaces = 0; spaces < MAX_ROWS - row; spaces++) {
            System.out.print(" ");
        }
        for (stars = 0; stars < row; stars++) {
            System.out.print("* ");
        }
        System.out.println("");
    }

    for (row = ((MAX_ROWS + 1) / 2); row < MAX_ROWS; row++) {
        for (spaces = 1; spaces < row; spaces++) {
            System.out.print(" ");
        }
        for (stars = 0; stars < MAX_ROWS - row; stars++) {
            System.out.print(" *");
        }
        System.out.println("");
    }
    System.out.println("Diamond, size " + MAX_ROWS + ".");

}

// HOLLOW SQUARE

int loopVariable = 0;  

private static final int MAX_ROWS

while(loopVariable < MAX_ROWS) {               <--- THIS IS THE SYNTAX ERROR LINE
    if(loopVariable == MAX_ROWS - 1 || 
            loopVariable == 0) {

        for(int x=0; x < MAX_ROWS; x++) { 
            System.out.print("* "); 
        } 
    } else { 
        for(int x=0; x < MAX_ROWS; x++ ) { 
            if(x == 0||x == MAX_ROWS-1) {
                System.out.print("* ");
            } else { 
                System.out.print(" "); 
            } 
        } 
    } 
    System.out.println();
    System.out.println("Hollow Square, size " + MAX_ROWS + ".");

    loopVariable++; 
} 

}

These are the errors it's giving me:

Multiple markers at this line - Syntax error on token "while", = expected - Syntax error, insert ";" to complete FieldDeclaration

Thanks in advance!

2
  • 1
    ; after the line that is previous to your while loop. Commented Apr 11, 2014 at 2:36
  • 2
    It's in the middle of nowhere. Commented Apr 11, 2014 at 2:36

2 Answers 2

2

You did not put the semicolon on the previous line. The error is not on the while loop line, but the compiler only noticed the error when it got to that line.

Insert a ';' after private static final int MAX_ROWS and it should work.

EDIT: You will also need to remove "final" from the declaration if you plan to change the value of "MAX_ROWS" later in the code. You cannot change a "final" value. Furthermore, as someone mentioned, you will need to initialize it, for example by setting it equal to 0. You can then change it later.

Sign up to request clarification or add additional context in comments.

Comments

2

Put an semicolon (';') at end end of the previous line, like this:

private static final int MAX_ROWS;

3 Comments

I think you and I posted the exact same thing at the exact same time--looking at the timestamps XD (+1 by the way since it's still the right answer)
I've tried that, but it gives me this error instead: Syntax error on token ";", { expected after this token
You cannot have a "final" int and then try to change it (which you do in your code). Try just using private static int and make sure to initialize it with a value (i.e. "0"). Then you can change it later on in your code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.