Java try/catch-one";返回isn';t found";或;变量为';t初始化为“;?
我已经盯着这个看了好几个小时,想不出解决办法;我通常用正则表达式处理这种类型的验证,但我尝试使用内置的解决方案进行更改(显然,我不经常这样做):Java try/catch-one";返回isn';t found";或;变量为';t初始化为“;?,java,compiler-errors,Java,Compiler Errors,我已经盯着这个看了好几个小时,想不出解决办法;我通常用正则表达式处理这种类型的验证,但我尝试使用内置的解决方案进行更改(显然,我不经常这样做): 这样做的错误是编译器找不到“return”,所以我得到了一个编译错误。如果我将“return”放在try/catch之外,我需要声明/初始化“input2”,这会破坏操作的目的。非常感谢您的帮助…让您的方法引发异常或返回nan。在catch部分引发异常。无论您在何处调用promptUserDecimal方法,都要捕获任何异常并在那里打印消息: publ
这样做的错误是编译器找不到“return”,所以我得到了一个编译错误。如果我将“return”放在try/catch之外,我需要声明/初始化“input2”,这会破坏操作的目的。非常感谢您的帮助…让您的方法引发异常或返回nan。在
catch
部分引发异常。无论您在何处调用promptUserDecimal
方法,都要捕获任何异常并在那里打印消息:
public static void main(String[] args) {
double d = 0.0;
while (double == 0) {
try {
d = promptUserDecimal();
} catch (NumberFormatException e) {
//log the message...
d = 0.0;
}
}
}
private static double promptUserDecimal() throws NumberFormatException {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a decimal");
return Double.parseDouble(scan.nextLine());
}
这是一种更好的方法,因为您让
promptUserDecimal
只关心处理读取双精度值。您必须尝试将每个类和方法按照其设计的特定目的进行分离。您需要以下内容:
double input2;
try{
//read input2
}catch( ... ){
//... log AND assign a value to input2 in case of invalid input
}
return input2;
您可以在catch块中抛出异常。i、 e
private static double promptUserDecimal() throws OopsException {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a decimal");
try{
double input2 = Double.parseDouble(scan.nextLine());
return input2;
} catch(NumberFormatException e){
System.out.println("Sorry, you provided an invalid option, please try again.");
throw new OopsException();
}
}
然后,每次他们给出无效输入时,您都可以捕获它并在调用该方法的位置处理它。private static double promptUserDecimal(){
private static double promptUserDecimal(){
Scanner scan = new Scanner(System.in);
System.out.println("Enter a decimal");
double input2 = 0.0; // <-- explicit initialization
try{
double input2 = Double.parseDouble(scan.nextLine());
} catch(NumberFormatException e){
System.out.println("Sorry, you provided an invalid option, please try again.");
}
return input2;
}
扫描仪扫描=新扫描仪(System.in);
System.out.println(“输入小数”);
double input2=0.0;//如果您希望用户“请重试”,听起来您需要一个循环:
private static double promptUserDecimal(){
final Scanner scan = new Scanner(System.in);
// Ask for input until we get something valid
while (true) { // Terminated by return within
System.out.println("Enter a decimal");
try {
return Double.parseDouble(scan.nextLine());
} catch(NumberFormatException e){
System.out.println("Sorry, you provided an invalid option, please try again.");
// No return, so the loop will run again
}
}
}
您需要从(或在捕获之后)返回或抛出某些内容。根据您对用户的输出判断,您似乎只想再次执行相同的操作。只需再次调用该方法并返回结果
private static double promptUserDecimal(){
Scanner scan = new Scanner(System.in);
System.out.println("Enter a decimal");
try{
double input2 = Double.parseDouble(scan.nextLine());
return input2;
} catch(NumberFormatException e){
System.out.println("Sorry, you provided an invalid option, please try again.");
return promptUserDecimal();
}
}
列出的一些解决方案将解决编译器问题,但第一步是后退一步,询问“如果出现NumberFormatException,我想做什么?”
一种方法是通过重新引用NumberFormatException或将其包装在RuntimeException中以使其不被选中来传播异常。这意味着调用代码将必须处理它,否则用户将看到stacktrace。如果您这样做,您甚至不需要在方法中使用try-catch。您只需声明对方法签名“抛出NumberFormatException”,并让它在上游得到处理
另一个选项是返回null,可以使用“return null”作为catch块中的最后一条语句,也可以将null作为方法中的最后一条语句。这是一个糟糕的选项,因为调用代码和/或最终用户无法获得他们需要的信息,即“输入了一个非数字”
我将使用选项一,并通过告诉用户scan.nextline+“未被识别为有效的double来处理异常。“您捕获了异常,打印了一条消息,但该方法仍然从catch
块继续。编译器抱怨,如果命中catch块,您不会返回(不是所有路径都返回).1但是如果你无论如何都要抛出NumberFormatException
,那么你就不需要捕捉它。不要首先捕捉它,让parseDouble
抛出异常。我有一个问题。这个代码段编译?OP:我喜欢这个无人参与脚本的解决方案。你可以在声明变量时指定默认的错误值使用递归进行“重试”有点奇怪;每次都会在堆栈上建立更多的挂起调用。虽然用户不太可能输入足够多的错误输入次数来导致堆栈溢出,但循环更有意义。
private static double promptUserDecimal(){
Scanner scan = new Scanner(System.in);
System.out.println("Enter a decimal");
try{
double input2 = Double.parseDouble(scan.nextLine());
return input2;
} catch(NumberFormatException e){
System.out.println("Sorry, you provided an invalid option, please try again.");
return promptUserDecimal();
}
}