Integer.parseInt抛出的NumberFormatException [英] NumberFormatException thrown by Integer.parseInt

查看:826
本文介绍了Integer.parseInt抛出的NumberFormatException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿我在学校上课,但是老师没有解释得那么好,所以我们必须在网上查找我做过的信息,但我无法在我的代码中找到错误,你能帮帮我吗? / p>

Hey Im taking coding lessons at school but the teacher does not explain that well so we have to look for info online which I did, but I was not able to find the error in my code, can you help me please?

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: 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)


推荐答案

请参阅解析 作为整数将抛出 NumberFormatException 。您必须按此顺序检查 null isEmpty(),然后尝试将字符串解析为整数。

See, you are trying to parse "" as an Integer whichwill throw NumberFormatException. You have to check for null and isEmpty() in this order and then try to parse the string as an integer.

这篇关于Integer.parseInt抛出的NumberFormatException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆