Java 语法错误,插入“EnumBody”完成BlockStatement
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21896538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax error, insert "EnumBody" to complete BlockStatement
提问by Sixela_Nna080597
public class SumLineTester {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SumLine test = new SumLine("5 8 3 2 1");
System.out.println(test.getSum());
}
}
import java.util.Scanner;
public class SumLine {
private String line;
public SumLine(String s) {
line = s;
}
public int getSum(){
int sum = 0;
Scanner chop = new Scanner(line);
while(chop.hasNextInt()){
sum= sum + chop.nextInt();
}
return sum;
public String getLine(){
return s;
}
public String toString(){
return line;
}
}
}
The first portion of code above import is my tester class that contains my main method. The rest is the code that I am trying to run off of tester and I keep getting this error:
上面 import 代码的第一部分是我的测试器类,其中包含我的主要方法。其余的是我试图运行测试仪的代码,我不断收到此错误:
Syntax error, insert "EnumBody" to complete BlockStatement
at SumLine.getSum(SumLine.java:17)
at SumLineTester.main(SumLineTester.java:10)
How do I fix the bug?
我该如何修复这个错误?
回答by mdewitt
You are missing the close brace at the end of your getSum()
你错过了结尾处的大括号 getSum()
public int getSum(){
int sum = 0;
Scanner chop = new Scanner(line);
while(chop.hasNextInt()){
sum= sum + chop.nextInt();
}
return sum;
^ \no close brace
public String getLine(){
return s;
}
This throws the exception because you are trying to declare a new method inside your getSum()
method which is not allowed.
这会引发异常,因为您试图在您的getSum()
方法中声明一个不允许的新方法。
Just add the close brace in like this:
只需像这样添加右括号:
public int getSum(){
int sum = 0;
Scanner chop = new Scanner(line);
while(chop.hasNextInt()){
sum= sum + chop.nextInt();
}
return sum;
}
^ \close brace added
public String getLine(){
return s;
}
Also, as David Wallace noted in the comments, don't forget to remove the extra curly bracket at the end of your class.
此外,正如大卫华莱士在评论中指出的那样,不要忘记在课程结束时删除额外的大括号。
As a side note, if you use an IDE like Eclipse, these errors are a bit easier to spot.
附带说明一下,如果您使用像 Eclipse 这样的 IDE,这些错误会更容易发现。
回答by dlb
Not at all sure why people think it is appropriate to downvote a question for some reason thinking the question is beneath them or such. This is a perfectly valid question. Even the most experienced programmers make syntactical errors. It so happens I was lead here for a similar error, a ";" to terminate an annotation line which resulted in the same message.
完全不知道为什么人们认为出于某种原因认为该问题在他们之下或诸如此类的问题下投票是合适的。这是一个完全有效的问题。即使是最有经验的程序员也会犯语法错误。碰巧我因为类似的错误而被带到这里,一个“;” 终止导致相同消息的注释行。
The Syntax error, insert "EnumBody" to complete BlockStatementseems to be a default error message for is a syntax error was made which confused the compiler badly enough that it could not tell what you were trying to do. Once I understood this, was easy to spot that I ended a line with a ; that I should not have, and if/when I see this message again I will know it means I made a rookie syntax error and will no the class of error to look for. Such an explanation would have helped this user, and if such an answer is beneath users, bypass the question please. Downvotes are for bad questions and answers, not for questions on errors that you are too good to ever make.
在语法错误,插入“EnumBody”,以完整的块语句似乎是默认的错误消息语法错误作出这混淆了编译器严重,以至于它不能告诉你试图做。一旦我理解了这一点,就很容易发现我以 ; 结束了一行。我不应该有,如果/当我再次看到这条消息时,我会知道这意味着我犯了一个新手语法错误,并且没有要查找的错误类别。这样的解释会帮助这个用户,如果这样的答案在用户之下,请绕过这个问题。Downvotes 是针对糟糕的问题和答案,而不是针对你永远不会犯的错误的问题。