Java 案例被跳过并发送到catch?
运行此代码时,跳过案例6并将其发送到catch语句 用户应该在一行输入他/她的全部输入(例如6 4 10 enter),然后数字应该被StringTokenizer拆分,然后添加到while语句中,但是会被发送到打印该语句的catch 下面是我的consoleReader类,解释它的功能和引发的异常,以及我程序中的案例6Java 案例被跳过并发送到catch?,java,string,tokenize,Java,String,Tokenize,运行此代码时,跳过案例6并将其发送到catch语句 用户应该在一行输入他/她的全部输入(例如6 4 10 enter),然后数字应该被StringTokenizer拆分,然后添加到while语句中,但是会被发送到打印该语句的catch 下面是我的consoleReader类,解释它的功能和引发的异常,以及我程序中的案例6 case 6: System.out.println("Enter your numbers all on one line
case 6:
System.out.println("Enter your numbers all on one line then press enter");
String pnum = console.readLine();
StringTokenizer tokenizer = new StringTokenizer(pnum);
double sum = 0;
while(tokenizer.hasMoreTokens()){
double num = Double.parseDouble(pnum);
sum = num + num;
}
mathOut = String.valueOf(sum);
break;
}
}catch(NumberFormatException e){
System.out.println("Your number was incorrect, please try again, enter \"E\" to exit or enter to continue.");
String continueOrQuit = console.readLine();
if(continueOrQuit.equalsIgnoreCase("e")){
done = true;
}else{
done = false;
}
控制台阅读器:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
/**
A class to read strings and numbers from an input stream.
This class is suitable for beginning Java programmers.
It constructs the necessary buffered reader,
handles I/O exceptions, and converts strings to numbers.
*/
public class ConsoleReader
{ /**
Constructs a console reader from an input stream
such as System.in
@param inStream an input stream
*/
public ConsoleReader(InputStream inStream)
{ reader = new BufferedReader
(new InputStreamReader(inStream));
}
/**
Reads a line of input and converts it into an integer.
The input line must contain nothing but an integer.
Not even added white space is allowed.
@return the integer that the user typed
*/
public int readInt()
{ String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
/**
Reads a line of input and converts it into a floating-
point number. The input line must contain nothing but
a nunber. Not even added white space is allowed.
@return the number that the user typed
*/
public double readDouble()
{ String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
/**
Reads a line of input. In the (unlikely) event
of an IOException, the program terminates.
@return the line of input that the user typed, null
at the end of input
*/
public String readLine()
{ String inputLine = "";
try
{ inputLine = reader.readLine();
}
catch(IOException e)
{ System.out.println(e);
System.exit(1);
}
return inputLine;
}
private BufferedReader reader;
}
您正在尝试转换原始字符串输入,而不是令牌: 更改此项:
while(tokenizer.hasMoreTokens()){
double num = Double.parseDouble(pnum);
sum = num + num;
}
据此:
while(tokenizer.hasMoreTokens()){
String token = tokenizer.nextToken();
double num = Double.parseDouble(token);
sum += num;
}
只有在抛出catch获取的异常时,才能输入
catch
。查看stacktrace。如果某个内容“发送到catch
而不是case 6:
”,则意味着在程序到达case 6:
之前,您已经遇到异常。这是它根本不应该进入catch的内容。因此,在catch中调用e.printStackTrace()
,以便您可以看到异常是什么,它来自哪里,等等。已经有一个类“从输入流中读取字符串和数字”,名为Scanner
。这非常有效!非常感谢,我应该意识到这一点。