vendredi 22 février 2019

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.

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

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. If it is an integer from 0 to 100 then I want to do other stuff. And if its neither of them, then I want the "else" statement to work.

The issue that I can't get the "else" statement to work as I get the NumberFormatException error. For example if I enter some other letter i.e. "d" - I get this error message:

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)

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.

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

           }

Aucun commentaire:

Enregistrer un commentaire