#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Manila, Philippines
    Posts
    33
    Rep Power
    6

    Loop While Exception is Being Thrown


    Hi guys,

    I want to say "while InputMismatchException, ask for input again".

    Here is my code:
    Code:
    /** BEGIN CREDIT CARD INPUT **/
    boolean isNumber = false;
    while(!isNumber) {
    	System.out.print(Message.ASK_CC_NUMBER);
    	try {
    		longCardNumber = scan.nextLong();
    			
    		while(longCardNumber <=0) {
    			System.out.print("Number cannot be 0 or negative.\n\n");
    			System.out.print(Message.ASK_CC_NUMBER);
    			longCardNumber = scan.nextLong();
    		}
    				
    		isNumber = true;
    			
    	} catch(InputMismatchException ime) {
    		System.out.print("Only digits allowed. "
    			+ "Number cannot have spaces or hyphens.");
    		continue;
    	}
    }
    /** END CREDIT CARD INPUT **/
    However, when I enter a letter for a credit card number to incite the exception, it enters an infinite loop.

    Assistance is much appreciated.

    Minor Update: Left out a } for the while(!isNumber) loop when I copied it, but that's not really the problem.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,737
    Rep Power
    352
    Which while() loop is the problem?
    NOTE: A while loop continues execution until its condition goes false;
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Manila, Philippines
    Posts
    33
    Rep Power
    6
    I believe the outer while(!isNumber) is the problem, because when I enter 0 or negative, it loops okay and exits when a positive number is entered.

    I am unsure how to handle looping input with exceptions.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,737
    Rep Power
    352
    I assume that you want the loop to continue when there is an exception because of bad data to allow the user to correct it.

    Can you post the program's output that shows the problem? Add some comments saying what you want to happen.

    Look at what the nextLong() method does. It doesn't process the input if it is not a long. If the input doesn't change, the loop doesn't end.

    Try reading in with next() to clear the input buffer
    or read with next() and use the parseLong() method
    or use the hasNextLong() method before using the nextLong() method.

    Comments on this post

    • kathy025 agrees : Forgot to thank you. Clearing the input buffer was the key.
    Last edited by NormR; April 11th, 2014 at 12:08 PM.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Manila, Philippines
    Posts
    33
    Rep Power
    6
    Originally Posted by NormR
    I assume that you want the loop to continue when there is an exception because of bad data to allow the user to correct it.
    Yes, that is exactly my intention.

    Message.ASK_CC_NUMBER is a String that says "Enter credit card number: "

    After I enter let's say, "x", my catch message, "Only digits allowed. Number cannot have spaces or hyphens.", appears then the Console clears and nothing else appears. It will only stop when I click the red Stop button in Eclipse. Unfortunately, that's why I can't take a proper screenshot of it.

    What I intend to happen is this:
    Code:
    Enter credit card number: x
    Only digits allowed. Number cannot have spaces or hyphens.
    
    Enter credit card number:
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,737
    Rep Power
    352
    I had several suggestions in post#4. Try one of those.

    For debugging a infinite loop, change the code like this:
    Code:
          int cnt = 0;  //<<<<<  Debug to stop infinite loop
    
          while(!isNumber && (cnt++ < 5)) {
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Manila, Philippines
    Posts
    33
    Rep Power
    6
    I fixed it! I added this to the catch block:
    Code:
    scan.nextLine();
    isNumber = false;
    I also removed continue since it's not needed.

    Thanks!
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Manila, Philippines
    Posts
    33
    Rep Power
    6
    This also helped me solve the problem.

IMN logo majestic logo threadwatch logo seochat tools logo