10

I coded an enum type which brings up the following Syntax errors when I run my created JUnit test for it:

java.lang.Error: Unresolved compilation problems: 
    Syntax error, insert "enum Identifier" to complete EnumHeaderName
    Syntax error, insert "EnumBody" to complete EnumDeclaration
    Syntax error, insert "}" to complete ClassBody

My enum type has static functions which for a particular String, returns an enum constant. Here is some of my code of the enum type:

public enum MusicType {

    ACCIDENTAL, LETTER, OCTAVE, REST, DUR, CHORD, TUPLET;

    public static MusicType is_accidental(String a){
        if (a=="^" | a=="_"|a=="=")
            return ACCIDENTAL;
        else return null;
    }

}

The rest of my static functions are very similar (i.e. is_letter, is_octave, etc.), although some use input.matches(regex) function instead of checking to see if an input it equals a particular string.

Here is the beginning of the JUnit test which tests the function dealing with the accidental constant:

public class MusicTypeTest {

    @Test
    public void accidentalTest(){
        String sharp = "^";
        String flat = "_";
        String natural = "=";
        assertEquals(MusicType.ACCIDENTAL, MusicType.is_accidental(sharp));
        assertEquals(MusicType.ACCIDENTAL, MusicType.is_accidental(flat));
        assertEquals(MusicType.ACCIDENTAL, MusicType.is_accidental(natural));
    }

}

The other functions in my JUnit test which test all the enum static functions are coded similarly. I cannot figure out why I have these syntax errors (this is my first time coding an enum type). I've been coding in Eclipse and have not found any missing "}"s as of yet. I don't know if this has anything to do with the way I've written the test or the way I've declared my variables. Does anyone know why I have these syntax errors?

Betlista
  • 10,327
  • 13
  • 69
  • 110
tkrishnan
  • 327
  • 3
  • 4
  • 11
  • 1
    You are not missing a closing `}`, are you? You just did not paste it into the code fragment, right? – Sergey Kalinichenko Apr 03 '12 at 02:43
  • 3
    Not directly related to your question, but instead of `if (a=="^" | a=="_"|a=="=")` you need to write `if (a.equals("^") || a.equals("_") || a.equals("="))`, or better yet `if(a.matches("^[_=^]$"))` – Sergey Kalinichenko Apr 03 '12 at 02:47
  • No the closing braces are there! I just didn't post it into the code fragment. Also yes, you are absolutely right: the "==" should be .equals or I should use .matches(regex). – tkrishnan Apr 03 '12 at 04:26
  • You have to add complete code if you want some help. Source files above are both ok. When I remove '}' from your MusicType enum I'm getting `Syntax error, insert "}" to complete EnumBody MusicType.java /StackOverflow/src line 10 Java Problem`, please add similar error messages (You can copy these from Eclipse problems view when you select error and press CTRL + C). – Betlista Apr 03 '12 at 10:23

4 Answers4

16

I had this same problem with Eclipse. It was a misleading syntax error message. It was due to a misplaced ";" after an annotation.

Double check your code ignoring the message.

neves
  • 33,186
  • 27
  • 159
  • 192
  • 1
    I got the same error message from the following junit code. What a useless and misleading error message... "WithMockUser(username="user", roles="role");" – Greg Jun 27 '21 at 22:05
3

I was getting this error while writing an Android app. All my brackets were closed; I was following an example from a different site. I ended up selecting the entire text for my code, cutting, saving, and pasting the code back. The error went away. It's very possible that Eclipse got stuck...

wileyCoyote
  • 829
  • 5
  • 8
3

Both the enum type and the class that you have just posted have two opening braces ({) and only one closing brace (}). If I had to guess, I'd say you need to put one more closing brace at the end of each of these files.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • Sorry the braces are there! I just didn't post the entire enum/left them out of the code fragment. My apologies! – tkrishnan Apr 03 '12 at 04:27
  • 1
    @tkrishnan: It's likely that these errors are resulting from other parts of the files that you haven't included. Maybe you should try posting the whole code files? (If the code files are too big to post here, try removing code from them until you have a bare minimum that still reproduces your errors. If you end up eliminating the errors in this way, that should at least help you figure out where the problem is.) – StriplingWarrior Apr 03 '12 at 04:33
0

I'm having the same issue at this line of code @GeneratedValue(strategy = GenerationType.IDENTITY). I deleted that line and I clean and build the project. Then I wrote it again and clean&built the project one more time. The error is gone.

Zin Win Htet
  • 2,448
  • 4
  • 32
  • 54