0

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

           }
  • are you trying to get a numerical value for letters, as your variable names suggest? – Stultuske Feb 22 '19 at 11:26
  • Possible duplicate of [Good way to encapsulate Integer.parseInt()](https://stackoverflow.com/questions/1486077/good-way-to-encapsulate-integer-parseint) – pringi Feb 22 '19 at 11:27

2 Answers2

2

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(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) && ...
f1sh
  • 11,489
  • 3
  • 25
  • 51
  • if that's what he's looking for, but looking at the code, that's not clear – Stultuske Feb 22 '19 at 11:26
  • I think it's pretty clear. He checks for `r` or `R` and after that just attempts to parse it. That's why it fails at `d`. – f1sh Feb 22 '19 at 11:27
  • I understand the error. But considering the variable names he has, he is trying to read letters, not numbers, so he may actually be looking for a numerical value for d – Stultuske Feb 22 '19 at 11:27
0

Short answer: docs.oracle.

Complete answer: You can use Integer.parsInt (String s) only on a string that can be parserized into an integer. The letter "R" can not be a number, so it generates an exception.

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
}
Alessandro
  • 31
  • 5
  • actually, the error is on letter "d". though your answer solves the exception, it might just as wel be that he was looking for int number = 'd'; – Stultuske Feb 22 '19 at 12:03
  • Yes, but I simply showed him how to write the code for what he wanted to do. – Alessandro Feb 25 '19 at 08:28
  • you showed him how to avoid throwing an error when it's not a numerical value, but he may have wanted 'd' to return 100 (the numerical value for char d), – Stultuske Feb 25 '19 at 08:30