Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: exception question

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default exception question

    Is the call to getNum(in) in retry(in) a recursive call??

    What I'm trying to do is keep requesting input when NumberFormatException
    is thrown.

    import java.io.InputStreamReader;
    import java.io.BufferedReader;
    import java.io.IOException;
     
    public class test
    {
       private static int retry(BufferedReader in) throws IOException
       {
          System.out.print("Enter number: ");
          return test.getNum(in);
       }
     
       public static int getNum(BufferedReader in) throws IOException
       {
          int num;
           try
           {
              num = Integer.parseInt(in.readLine());
           }
           catch(NumberFormatException e)
           {
              System.out.println("INVALID INPUT!!!\n");
              num = retry(in);  
           }
          return num;
       }
     
     
       public static void main(String[] args) 
         {
            BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
            String name;
            int num1, num2;
     
          try
            {
              System.out.print("Enter a line: ");
              name = input.readLine();
              System.out.println("Hello : " + name);
     
              System.out.print("Enter first number: ");
              num1 = test.getNum(input);
     
              System.out.print("Enter second number: ");
              num2 = test.getNum(input);
     
              System.out.println(name + " the answer is " + (num1 + num2));
            }
          catch(IOException e)
            {
              System.out.println("IO ERROR!!!!\n" + e.getMessage() + "\n");
            }
     
         }
     
    }


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: exception question

    Yes, it is a recursive call. Seems to work OK - what's the problem?

    If you find recursion awkward to follow, you could loop instead, and it would be nice to make the prompts clearer to the user by repeating the prompt and input until valid input is received, e.g.
    prompt = "Enter first number"
    input = getInput(prompt)
    while not valid input
      input = getInput(prompt)
    end while

Similar Threads

  1. Quick question with throwing and catching an exception.
    By Andyandhisboard in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 12th, 2011, 10:24 PM
  2. [SOLVED] Should I use an exception here?
    By whity in forum Java Theory & Questions
    Replies: 3
    Last Post: May 4th, 2011, 06:52 AM
  3. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM
  4. can someone please help me im getting exception
    By kristynrod in forum Exceptions
    Replies: 2
    Last Post: March 15th, 2011, 04:40 PM
  5. DAO exception
    By nrao in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 13th, 2010, 12:22 PM