1 2 Previous Next 24 Replies Latest reply: Feb 16, 2008 4:57 AM by 800308 RSS

    try catch NumberFormatException?

    807601
                                    
                                                    try{
                                    //Questionnaire Number input
                                    System.out.print("Enter Questionnaire Number [ ] "); 
                                    inputQuestionnAireNum = Integer.parseInt(input.nextLine());
                               
                                    
                                    } catch (NumberFormatException e){
                                         System.out.print("Error"); 
                                    
                                    }
      hi how do i use try catch NumberFormatException?

      i like to validate that the variable to only accept int and not String or other combination of character. how can i do so with try and catch ? and i need it to loop back to Enter Questionnaire again if the input is invalid. Do i use a do while loop for that ?
        • 1. Re: try catch NumberFormatException?
          807601
          yes, use a do while loop. and I don't see what's wrong with what you have.
          • 2. Re: try catch NumberFormatException?
            807601
            in your catch block, put a call to the method that contains the try-catch block, so it will keep calling until it doesn't get a numberformatexception
            • 3. Re: try catch NumberFormatException?
              807601
              hi could u show me a simple example to put a call to the method that contains the try-catch block ?
              • 4. Re: try catch NumberFormatException?
                807601
                public int getIntegerFromUser()
                {
                  try
                  {
                    return Integer.parseInt(input.readLine());
                  }
                  catch (NumberFormatException e)
                  {
                    System.out.print("I'm sorry, that wasn't a number.  Try again: ");
                    return getIntegerFromUser();
                  }
                }
                • 5. Re: try catch NumberFormatException?
                  807601
                                    //Questionnaire Number 
                                      do {
                                      System.out.print("Enter Questionnaire Number [ ] "); 
                                        try
                                        {
                                             inputQuestionnAireNum = Integer.parseInt(input.nextLine());
                                        }
                                        catch (NumberFormatException e)
                                        {
                                          System.out.print("I'm sorry, that wasn't a number.  Try again: ");
                                        }
                                      } while (???);
                  hi what condition should i put in order to make in loop ? forgive me i am very new to java. thank you
                  • 6. Re: try catch NumberFormatException?
                    807601
                                      //Questionnaire Number 
                                        do {
                                        System.out.print("Enter Questionnaire Number [ ] "); 
                                          try
                                          {
                                               inputQuestionnAireNum = Integer.parseInt(input.nextLine());
                                                              break;
                                          }
                                          catch (NumberFormatException e)
                                          {
                                            System.out.print("I'm sorry, that wasn't a number.  Try again: ");
                                          }
                                        } while (true);
                    • 7. Re: try catch NumberFormatException?
                      807601
                      alternatively you could be more verbose...
                                        //Questionnaire Number
                                                      boolean amIDoneYet = false;
                                          do {
                                          System.out.print("Enter Questionnaire Number [ ] "); 
                                            try
                                            {
                                                 inputQuestionnAireNum = Integer.parseInt(input.nextLine());
                                                                // what do you think you should do here?
                                            }
                                            catch (NumberFormatException e)
                                            {
                                              System.out.print("I'm sorry, that wasn't a number.  Try again: ");
                                            }
                                          } while (!amIDoneYet);
                      • 8. Re: try catch NumberFormatException?
                        807601
                        hi i have tried your method it work however like what you mention i think need to add something which i have tried but its not working.. it jus keep repeating and nv get out of do-while loop. what should i return or what i miss out ?? thnaks in advance !
                        • 9. Re: try catch NumberFormatException?
                          807601
                          hi i add break; and it work, correct me if i am wrong. thanks!
                          • 10. Re: try catch NumberFormatException?
                            807601
                            need to add something which i have tried
                            What have you tried?

                            The loop will keep going until you do something that makes amIDoneYet   {color: #0000ff}true{color}.

                            Edited by: pbrockway2 on Feb 16, 2008 9:37 PM

                            ... or break out of the loop. But I think it's better to do something with the loop variable.
                            • 11. Re: try catch NumberFormatException?
                              807601
                              hi, i have another question, how do i validate that the integer in not negative and is between a certain range of number inside try and catch ?

                              please show me example it helps me alot thanks~!
                              • 12. Re: try catch NumberFormatException?
                                807601
                                       try
                                                      {
                                                           inputQuestionnAireNum = Integer.parseInt(input.nextLine());
                                                                        break;
                                                      }
                                hi i added break; and it work. please correct me if i am wrong, cause i not really sure if this is the right way.
                                • 13. Re: try catch NumberFormatException?
                                  807601
                                  metaroot wrote:
                                  hi, i have another question, how do i validate that the integer in not negative and is between a certain range of number inside try and catch ?

                                  please show me example it helps me alot thanks~!
                                  I guess you could put the break inside an if statement. What class is this?
                                  • 14. Re: try catch NumberFormatException?
                                    807601
                                    hi i can`t seems to change amIDoneYet to true, please guide me
                                    1 2 Previous Next