使用Integer.parseInt时出现Java NumberFormatException

使用Integer.parseInt时出现Java NumberFormatException,java,if-statement,parseint,Java,If Statement,Parseint,在来到这里之前,我确实在互联网上搜索过答案,我认为答案将与try/catch语句有关,但即使在观看了有关该主题的几篇教程之后,我也不确定如何实现这一点 无论如何,我正在尝试在我正在制作的“新手提醒”应用程序中做一件简单的事情(现在我学习Java作为我的第一语言已经有3个月了) 我想让程序检查用户的输入,如果是某个字母(“R”),我想让程序做某些事情。如果它是一个从0到100的整数,那么我想做其他事情。如果两者都不是,那么我希望“else”语句起作用 问题是,当我得到NumberFormatExc

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

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

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

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

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

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

如果有人能告诉我代码有什么问题,以及我将如何达到我的目的,我将不胜感激。一些新来者友好的解决方案将不胜感激

提前谢谢你

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

           }
String secondLetter=mash.nextLine();
if(secondLetter.equals(“r”)| secondLetter.equals(“r”)){//如果用户输入r-创建新提醒
newRem();
}
else if((Integer.parseInt(secondLetter)>=0)和&(Integer.parseInt(secondLetter)<最大值)){//如果用户输入数字-检查任务列表
tasks.remText(Integer.parseInt(secondLetter));
System.out.println(“输入'D'将提醒设置为完成。或输入'T'返回列表”);
字符串v=mash.nextLine();
系统输出打印Ln(v);
如果(v.equals(“d”)| v.equals(“d”){//如果用户输入d-将提醒设置为完成
tasks.setDone(Integer.parseInt(secondLetter));
System.out.println(“提醒现在添加到“完成”列表中”);
}
else if(v.equals(“t”)| v.equals(“t”){//如果用户输入t-返回提醒列表
tasks.display();
}
否则{
System.out.println(“请输入正确的符号”);
}
}
否则{
System.out.println(“输入正确的符号”);
}

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

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) && ...

您可以在转换输入之前检查输入是否有效。例如:

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) && ...
简短答复:

完整答案: 只能对可以解析为整数的字符串使用Integer.parsInt(字符串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
}
if(Character.isleter(第二个字母)和&“R.equalsIgnoreCase(第二个字母)){
使用“R”进行编码
}else如果(Integer.parseInt(secondLetter)>0&&Integer.parseInt(secondLetter)<100){
执行0
简短回答:

完整答案: 只能对可以解析为整数的字符串使用Integer.parsInt(字符串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
}
if(Character.isleter(第二个字母)和&“R.equalsIgnoreCase(第二个字母)){
使用“R”进行编码
}else如果(Integer.parseInt(secondLetter)>0&&Integer.parseInt(secondLetter)<100){
执行0
如果这就是他想要的,但是看看代码,这并不清楚,我认为这很清楚。他检查
r
r
,然后尝试解析它。这就是它在
d
时失败的原因。我理解错误。但是考虑到他有变量名,他试着读字母,而不是数字,所以他实际上可能在寻找dif的数值,这就是他所寻找的,但看看代码,这并不清楚,我认为这很清楚。他检查
r
r
,然后尝试解析它。这就是它在
d
时失败的原因。我理解错误。但是考虑到他的变量名,他试图读取字母,而不是数字,所以他实际上可能在寻找一个数字值,你敢尝试获取字母的数字值吗,正如你的变量名所暗示的那样?可能重复的是你试图获取字母的数字值,正如您的变量名所表明的那样?实际上,可能是重复的,错误在字母“d”上。虽然你的答案解决了这个异常,但也可能是他在寻找int number='d';是的,但我只是向他展示了如何为他想做的事情编写代码。你向他展示了如何避免在不是数值时抛出错误,但他可能希望“d”返回100(char d的数值),实际上,错误在字母“d”上。虽然你的答案解决了这个异常,但也可能是他在寻找int number='d';是的,但我只是简单地向他展示了如何为他想做的事情编写代码