tags:

views:

66

answers:

1

I created a method and keep getting an error that I need to include a } at the end of my method. I put the } in and the error is still there! If I then delete that } the same error will pop up on a prior method; and that error wasn't there before. in other words, if i type the } on my most recent method then the error stays there and only there. if i delete it, it duplicates that error on my prior method.

private void putThreeBeepers() {
    for (int i = 0; i < 2; i++) {
        putBeeper();
        move();
    }
    putBeeper();
}

private void backUp() {
    turnAround();
    move();
    turnAround();
}
+1  A: 

You really want to go to the top of your file and do proper and consistent indention all the way to the bottom.

For example...

private void putThreeBeepers() 
{
    for (int i = 0; i < 2; i++) {
        putBeeper();
        move();
    }

    putBeeper();
}

private void backUp() 
{
    turnAround();
    move();
    turnAround();
}

Odds are, somewhere along the line, you are missing a }. Your description isn't super clear, but if the code you posted is how you actually have it formatted in your file then odds are you just missed something somewhere... and poor indentation makes it very hard to spot.

The fact that the message is changing is confusing, but it is the sort of thing you see in these cases.

TofuBeer
Btw, many IDEs and even plain text editors have an option to quickly match up braces, so you go to the top brace, press a certain key combination, and it shows you the matching brace. Learn this key combination. It's a good friend of yours.
EboMike
in my actual code, the formatting/indentation is perfect (i think...). I just had trouble transferring that code correctly to here.
LuxuryMode
thanks @ebomike. any idea what that is in Eclipse?
LuxuryMode
Using Eclipse, try Ctrl+Shift+F (you may want to save a backup copy if the source is not under version control) which should format the source code. Presumably any errors should crop up there (I don't use Eclipse so not sure what it will do if there are issues with the code).
TofuBeer
Ctrl+Shift+F didn't seem to do anything...
LuxuryMode
Syntax error, insert "}" to complete ClassBody. I'm confused. Why is it even referring to it as a class? Isn't a method?
LuxuryMode
Eclipse is CTRL+SHIFT+P by default. Look under Navigate -> Goto -> Matching Bracket. It's called ClassBody because it's the BODY of the CLASS. The missing bracket is for the "class", not for any of its methods. (It's implicit since one of your methods is missing a '}', so the final '}' that is supposed to terminate the class just terminates a method.)
EboMike
If you are on a Unix type machine (Linux, OSX, etc) or on Windows with Cygwin (which is free) do this: cat YourFileName.java | awk '{ for ( i=1; i<=length; i++ ) arr[substr($0, i, 1)]++ }END{ for ( i in arr ) { print i, arr[i] } }' | grep [{}] replace the YourFileName.java with the actual file name. This will tell you how many { and } there are... if the number isn't the same then you are missing one :-)
TofuBeer