min*_*i19 -1 java numberformatexception
嘿,我在学校上编程课,但老师没有很好地解释,所以我们必须在网上寻找我所做的信息,但我无法在我的代码中找到错误,你能帮我吗?
char end='s';
do{
System.out.println("Tipo de boleto");
char boleto = (char) System.in.read();
switch (boleto){
case 'a':
System.out.println("El boleto cuesta $120.00");
System.out.println("Otro boleto (s/n)?");
end = (char) Integer.parseInt(entrada.readLine());
continue;
case 'n':
System.out.println("El boleto cuesta $75.00");
System.out.println("Otro boleto (s/n)?");
end = (char) Integer.parseInt(entrada.readLine());
continue;
case 'i':
System.out.println("El boleto cuesta $60.00");
System.out.println("Otro boleto (s/n)?");
end = (char) Integer.parseInt(entrada.readLine());;
continue;
default:
System.out.println("Error" );
break;
}
}
while (end == 'n');
Run Code Online (Sandbox Code Playgroud)
例外
run: Tipo de boleto a El boleto cuesta $120.00 Otro boleto (s/n)?
Exception in thread "main" java.lang.NumberFormatException: For input string: "" at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:592) at
java.lang.Integer.parseInt(Integer.java:615) at
asjidbhahsjksbd.Asjidbhahsjksbd.main(Asjidbhahsjksbd.java:16) Java Result: 1
BUILD SUCCESSFUL (total time: 7 seconds)
Run Code Online (Sandbox Code Playgroud)
看,您正在尝试解析 ""为Integer which will throw NumberFormatException。您必须按此顺序检查null和isEmpty(),然后尝试将字符串解析为整数。
| 归档时间: |
|
| 查看次数: |
7647 次 |
| 最近记录: |