亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Integer.parseInt 時出現 Java NumberFormatException

使用 Integer.parseInt 時出現 Java NumberFormatException

呼喚遠方 2022-06-30 19:02:01
在來到這里之前,我確實在整個互聯網上搜索了答案,我認為答案與 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();               }
查看完整描述

2 回答

?
米琪卡哇伊

TA貢獻1998條經驗 獲得超6個贊

您可以在嘗試轉換之前檢查您的輸入是否為有效數字。例如:


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


查看完整回答
反對 回復 2022-06-30
?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

完整答案:您只能在可以解析為整數的字符串上使用 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

}


查看完整回答
反對 回復 2022-06-30
  • 2 回答
  • 0 關注
  • 366 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號