Results 1 to 6 of 6
  1. #1
    V4P
    V4P is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Post Try & Catch in While Loop doesn't work!

    I have been trying to get this simple try/catch block to repeat within a while loop using a boolean value. Every time there is no exception handled, the program works fine. When an exception is caught however, the catch block goes on to continuously repeat until the code is forced to stop. Here is the code:

    Java Code:
    import java.util.*;
    
    public class Main {
        public static void main(String[] args) {
    
            Scanner input = new Scanner (System.in);
            boolean loop = true;
    
            while(loop == true){
                try{
    		System.out.println("Enter a number:");
    		int usrInt = input.nextInt();
    		System.out.println("You wrote the number: " + usrInt);
    		loop = false;
                }
                catch(InputMismatchException e){
                    System.out.println("ERROR 01: Required type 'int' at line 12");
                }
    	}
        }
    }
    I want the code to continue looping after an exception is caught. I tried using "continue;" at the end of the catch block. It didn't work, and I found nothing on google that even slightly helped. Thanks for any help!
    Last edited by V4P; 03-02-2011 at 01:04 PM. Reason: grammar

  2. #2
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    That's correct. If you want to break out of the loop when you catch an Exception, then use break at the end of the catch block.

  3. #3
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Use input.nextLine() in the catch block.
    this clears the buffer.

  4. #4
    V4P
    V4P is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default Thanks

    thanks subith86. works perfectly. maybe i'm asking too much, but i like to learn. What do you mean by buffer and why would an empty scanner method fix my problem? you don't need to answer, just curiosity :). Thanks a lot either way!

  5. #5
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Well, when you use nextInt() to read an integer but you feed it with a String, it throws an exception. Now what I suppose (I'm not sure) is that, the string that you have entered is put in some buffer. When you try to read the second time using nextInt(), it doesn't actually go to read the new input, instead it reads the same String which is there in the buffer, entered by you earlier.

    That's all help I can offer. Even I started learning Java only 3 months back ;)

  6. #6
    V4P
    V4P is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    awesome. i only started a month ago. thanks for all your help and info.

Similar Threads

  1. Catch exception, return to while loop
    By ChicagoAve in forum New To Java
    Replies: 3
    Last Post: 02-20-2011, 06:44 PM
  2. try and catch inside a for loop fails!!!
    By Kingmaha64 in forum New To Java
    Replies: 3
    Last Post: 01-08-2011, 02:40 AM
  3. Try catch loop problems :'(
    By Romally in forum New To Java
    Replies: 7
    Last Post: 11-17-2010, 08:15 PM
  4. try-catch InputMismatchException in a while loop
    By themulator in forum New To Java
    Replies: 17
    Last Post: 10-12-2010, 04:49 AM
  5. Need help with getting second loop to work
    By twcast in forum New To Java
    Replies: 3
    Last Post: 02-08-2010, 02:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •