Java 标记错误、语法错误
我正在用Java编写一个函数,但我不能编译代码。我没有看到语法错误;你能帮我吗Java 标记错误、语法错误,java,syntax,Java,Syntax,我正在用Java编写一个函数,但我不能编译代码。我没有看到语法错误;你能帮我吗 public String getUserInput(String prompt){ String inputLine = null; System.out.print(prompt + " "); try( BufferedReader is = new BufferedReader(new InputStreamReader(System.in)); inp
public String getUserInput(String prompt){
String inputLine = null;
System.out.print(prompt + " ");
try(
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
inputLine = is.readLine();
if (inputLine.length() == 0 )
return null;
)
catch (IOException e) {
System.out.println("IOException: " + e);
}
return inputLine.toLowerCase();
}
它不编译。将try块替换为{and}并用{and}括起try块。请检查try块括号,该括号在java中是不允许的,并且是为方法保留的。对于块,请使用{} 正确答案
public String getUserInput(String prompt){
String inputLine = null;
System.out.print(prompt + " ");
try
//( this is wrong
{
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
inputLine = is.readLine();
if (inputLine.length() == 0 )
return null;
//)
}
catch (IOException e) {
System.out.println("IOException: " + e);
}
return inputLine.toLowerCase();
}
您是否想要Java 7+
如果不是,请回想标准块的形式:
try {
...
} catch (...) {
}
请注意{}括号。如果代码中有错误,如果您发布了错误,这将非常有用。StackOverflow不是检查语法错误的地方。为了便于将来参考,您不能在括号内执行语句列表,它必须在块{}内。特别是在开始Java编程时,我强烈建议使用IDE[NetBeans][例如,作为一个注释和一些您可能想添加到答案中的内容,try with resource仅在Java 7以后版本中可用。@ChrisForrence确实是我添加的。@arshajii+1表示try with resource。我只是想知道使用try with resource与旧的try-catch时尚相比有什么优势吗?@使用try with resources更简单。这在我的第一个链接中有所解释。@rocking没有性能差异。这只是为了程序员的方便。
try {
...
} catch (...) {
}