语法错误:插入“枚举标识符”,插入“ EnumBody”,插入“}”

tkr*_*nan 5 java eclipse junit enums syntax-error

我对一个枚举类型进行了编码,当我为其运行创建的JUnit测试时,它会引发以下语法错误:

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
Run Code Online (Sandbox Code Playgroud)

我的枚举类型具有静态函数,该函数针对特定的String返回枚举常量。这是我的一些枚举类型的代码:

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

我的静态函数的功能是非常相似的(即is_letteris_octave等),但也有一些使用input.matches(regex)功能,而不是检查,看看是否输入它等于一个特定的字符串。

这是JUnit测试的开始,该测试测试处理意外常量的功能:

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));
    }

}
Run Code Online (Sandbox Code Playgroud)

我的JUnit测试中测试所有枚举静态函数的其他函数都以类似的方式编码。我不知道为什么会有这些语法错误(这是我第一次编码枚举类型)。我一直在Eclipse中进行编码,到目前为止还没有发现任何缺少的“}”。我不知道这是否与编写测试的方式或声明变量的方式有关。有谁知道我为什么有这些语法错误?

nev*_*ves 10

我在 Eclipse 上遇到了同样的问题。错误的语法错误消息是由于错位的“;” 注释后。


小智 3

我在编写 Android 应用程序时遇到此错误。我所有的括号都关闭了;我正在关注另一个网站的示例。我最终为我的代码选择了整个文本,剪切、保存并粘贴了代码。错误消失了。Eclipse 很可能被卡住了......