简体   繁体   English

使用 Integer.parseInt 时的 Java NumberFormatException

[英]Java NumberFormatException when using Integer.parseInt

I have truly searched for the answer all over the Internet before coming here and I think that the answer will have something to do with the try/catch statements, but even after watching a couple tutorials on the topic I am not sure on how to implement that.在来这里之前,我真的在互联网上搜索了答案,我认为答案与 try/catch 语句有关,但即使在观看了有关该主题的几个教程后,我也不确定如何实现那。

Anyways, I am trying to do a simple thing in my newbie reminders app that I am making (I am learning Java as my first language for about 3 months now).无论如何,我正在尝试在我正在制作的新手提醒应用程序中做一件简单的事情(我现在学习 Java 作为我的第一语言大约 3 个月了)。

I want the program to check the user's input and if it is a certain letter ("R") I want the program to do a certain stuff.我希望程序检查用户的输入,如果它是某个字母(“R”),我希望程序执行某些操作。 If it is an integer from 0 to 100 then I want to do other stuff.如果它是从 0 到 100 的整数,那么我想做其他事情。 And if its neither of them, then I want the "else" statement to work.如果它们都不是,那么我希望“else”语句起作用。

The issue that I can't get the "else" statement to work as I get the NumberFormatException error.当我收到 NumberFormatException 错误时,我无法让“else”语句起作用的问题。 For example if I enter some other letter ie "d" - I get this error message:例如,如果我输入其他字母,即“d” - 我收到此错误消息:

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

Here is the code (I am sorry for any readability issues, this is the first time ever I am showing my code to somebody).这是代码(对于任何可读性问题,我很抱歉,这是我第一次向某人展示我的代码)。 You don't have to read the else if statement, as the issue seems to not depend on the text inside of that statement.您不必阅读 else if 语句,因为问题似乎与该语句中的文本无关。

I would really appreciate if anybody could point me what is wrong with the code and how I would get to do what I intended.如果有人能指出代码有什么问题以及我将如何做我打算做的事情,我将不胜感激。 Some newcomer-friendly solution will be much appreciated.一些新人友好的解决方案将不胜感激。

Thank you in advance!先感谢您!

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");

           }

You can check your input if it's a valid number before attempting to convert it. 您可以在尝试转换输入之前检查输入的数字是否有效。 For example: 例如:

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

place it in your if/else like this: 像这样将其放在您的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) && ...

Integer.parseInt will always throw an exception if you try to use it with something that is not an int . 如果您尝试将非intInteger.parseInt一起使用,则它将始终抛出异常。 And "d" is not an int . 而且“ d”不是int Therefore, what you should do is wrap that in a try/catch block, and prompt the user for another input if he inputs something invalid. 因此,您应该做的是将其包装在try / catch块中,如果用户输入的内容无效,则提示用户进行其他输入。

Short answer: docs.oracle. 简短答案: docs.oracle。

Complete answer: You can use Integer.parsInt (String s) only on a string that can be parserized into an integer. 完整答案:您只能在可以解析为整数的字符串上使用Integer.parsInt(字符串)。 The letter "R" can not be a number, so it generates an exception. 字母“ 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM