【问题标题】:Java NumberFormatException when using Integer.parseInt使用 Integer.parseInt 时出现 Java NumberFormatException
【发布时间】:2019-02-22 11:22:16
【问题描述】:

在来到这里之前,我确实在整个互联网上搜索了答案,我认为答案与 try/catch 语句有关,但即使在看了几个关于该主题的教程之后,我也不确定如何实现。

无论如何,我正在尝试在我正在制作的新手提醒应用程序中做一件简单的事情(我正在学习 Java 作为我的第一语言大约 3 个月)。

我希望程序检查用户的输入,如果它是某个字母(“R”),我希望程序执行某些操作。如果它是从 0 到 100 的整数,那么我想做其他的事情。如果它们都不是,那么我希望“else”语句起作用。

当我收到 NumberFormatException 错误时,我无法让“else”语句工作的问题。例如,如果我输入其他字母,即“d” - 我会收到以下错误消息:

线程“主”java.lang.NumberFormatException 中的异常:对于输入 字符串:“d”在 java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在 java.lang.Integer.parseInt(Integer.java:580) 在 java.lang.Integer.parseInt(Integer.java:615) 在 mash.Dialogue.startDialogue(Dialogue.java:51) 在 mash.Dialogue.newRem(Dialogue.java:27) 在 mash.Dialogue.startDialogue(Dialogue.java:38) 在 mash.Dialogue.start(Dialogue.java:13) 在 mash.Main.main(Main.java:9)

这是代码(对于任何可读性问题,我很抱歉,这是我第一次向别人展示我的代码)。您不必阅读 else if 语句,因为问题似乎不取决于该语句中的文本。

如果有人能指出代码有什么问题以及我将如何做我想做的事,我将不胜感激。一些对新手友好的解决方案将不胜感激。

提前谢谢你!

String secondLetter = mash.nextLine();
           if(secondLetter.equals("r") || secondLetter.equals("R")) {  //if the user enters R - create a new Reminder
             newRem();
    }
           else if((Integer.parseInt(secondLetter) >= 0) && (Integer.parseInt(secondLetter) < maximum)) { //if the user enters number - check task list
               tasks.remText(Integer.parseInt(secondLetter));
               System.out.println("Enter 'D' to set the reminder as Done. Or enter 'T' to return to the list");
               String v = mash.nextLine();
               System.out.println(v);
               if(v.equals("d")|| v.equals("D")) { //if user enters D - set the reminder as done
                   tasks.setDone(Integer.parseInt(secondLetter));
                   System.out.println("The reminder is now added to 'Done' list");
               }
               else if(v.equals("t")|| v.equals("T")) { //if user enters T - return to the list of reminders
                   tasks.display();

               }
               else {

                       System.out.println("Please enter the correct symbol");

               }
           }

           else {     
               System.out.println("Enter the correct symbol");

           }

【问题讨论】:

标签: java if-statement parseint


【解决方案1】:

您可以在尝试转换之前检查您的输入是否为有效数字。例如:

if(!secondLetter.matches("[0-9]+")) {
   //it's not a number, so dont attempt to parse it to an int
}

像这样将它放在你的 if/else 中:

if(secondLetter.equals("r") || secondLetter.equals("R")) {
  newRem();
} else if(!secondLetter.matches("[0-9]+")){
  System.out.println("please type r or R or a number");
} else if((Integer.parseInt(secondLetter) >= 0) && ...

【讨论】:

  • 如果这就是他要找的东西,但是看代码,就不清楚了
  • 我认为这很清楚。他检查rR,然后尝试解析它。这就是它在d 失败的原因。
  • 我理解错误。但考虑到他拥有的变量名称,他正在尝试读取字母,而不是数字,因此他实际上可能正在寻找 d 的数值
【解决方案2】:

简答:docs.oracle.

完整答案: 您只能在可以解析为整数的字符串上使用 Integer.parsInt (String s)。字母“R”不能是数字,所以会产生异常。

if(Character.isLetter(secondLetter) && "R".equalsIgnoreCase(secondLetter)){
   do code with "R"
}else if(Integer.parseInt(secondLetter) > 0 && Integer.parseInt(secondLetter) < 100){
   do code with 0 < number < 100
}else{
   do something else
}

【讨论】:

  • 实际上,错误在字母“d”上。尽管您的答案解决了异常,但他可能正在寻找 int number = 'd';
  • 是的,但我只是向他展示了如何为他想做的事情编写代码。
  • 您向他展示了如何避免在不是数值时引发错误,但他可能希望 'd' 返回 100(char d 的数值),
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-23
  • 2017-03-11
  • 1970-01-01
  • 2015-07-16
  • 2017-01-26
  • 2014-12-09
  • 1970-01-01
相关资源
最近更新 更多