Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By ShadowWalker

Thread: Help with InputCatchException ~

  1. #1
    CruelSoulz is offline Member
    Join Date
    May 2013
    Posts
    19
    Rep Power
    0

    Unhappy Help with InputCatchException ~

    I'm doing some input validation ... but i cannot figure out what's wrong with my code
    Java Code:
        	boolean stopLoop = false;
        	int YOS = 0;
        	do{
    	    	System.out.printf("%-20s : ", "Enter YearOfStudy");
    	    	try{
    				YOS = scanner.nextInt();
    	    	}
    	    	catch(java.util.InputMismatchException e){
    	    		System.out.println("-- Please Enter Integer Data --");
    	    		stopLoop = false;
    	    	}
    	    	
    	    	if(YOS>0 && YOS<5){
    	    		stopLoop = true;
    	    	}
    	    	
    	   	}while(stopLoop==false);
    When i enter "K", the looping will not stop until my JCreator crashed. It just keep on printing ( Enter YearOfStudy and -- Please Enter Integer Data --) but doesn't allow me to enter YOS ...

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: Help with InputCatchException ~

    What if do this?

    Java Code:
    	    try{
    		Scanner scanner = new Scanner(System.in);
    	        YOS = scanner.nextInt();
    	    }

  3. #3
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Help with InputCatchException ~

    I have to say that i don't really know why is this happening.. but i can give you a way to avoid or to correct the problem.. this work for me
    Java Code:
    import java.util.Scanner;
    import java.util.InputMismatchException;
    
    class MAIN{
    	public static void main(String args []){
    		boolean stopLoop = false;
    		int YOS = 0;
    		Scanner scanner = new Scanner(System.in);
    		do{
    			System.out.printf("%-20s : ", "Enter YearOfStudy");
    			try{
    				YOS = scanner.nextInt();
    				stopLoop = true;
    			}
    			catch(InputMismatchException e){
    				System.out.println("-- Please Enter Integer Data --");
    				scanner = new Scanner(System.in);
    			}
    			if(YOS>0 && YOS<5){
    				stopLoop = false;
    			}
    		}while(stopLoop==false);
    	}
    }
    but i am going to search why your program is wrong.. :P



    superhaNds you got me! :P xD

    last edit :

    here is what i found on the web:

    As per the javadoc for Scanner:

    When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.

    That means that if the next token is not an int, it throws the InputMismatchException, but the token stays there. So on the next iteration of the loop, reader.nextInt() reads the same token again and throws the exception again. What you need is to use it up. Add a reader.next() inside your catch to consume the token, which is invalid and needs to be discarded.

    in your situation
    Java Code:
    catch (InputMismatchException e) {
        System.out.println("-- Please Enter Integer Data --");
        scanner.next(); // this consumes the invalid token
    }
    Last edited by ShadowWalker; 07-14-2013 at 11:05 PM.
    CruelSoulz likes this.

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: Help with InputCatchException ~

    Aah yeah, that's the thing I couldn't remember, one has to discard the bad input otherwise it follows the bad forever.

  5. #5
    CruelSoulz is offline Member
    Join Date
    May 2013
    Posts
    19
    Rep Power
    0

    Default Re: Help with InputCatchException ~

    Quote Originally Posted by ShadowWalker View Post
    I have to say that i don't really know why is this happening.. but i can give you a way to avoid or to correct the problem.. this work for me
    Java Code:
    import java.util.Scanner;
    import java.util.InputMismatchException;
    
    class MAIN{
    	public static void main(String args []){
    		boolean stopLoop = false;
    		int YOS = 0;
    		Scanner scanner = new Scanner(System.in);
    		do{
    			System.out.printf("%-20s : ", "Enter YearOfStudy");
    			try{
    				YOS = scanner.nextInt();
    				stopLoop = true;
    			}
    			catch(InputMismatchException e){
    				System.out.println("-- Please Enter Integer Data --");
    				scanner = new Scanner(System.in);
    			}
    			if(YOS>0 && YOS<5){
    				stopLoop = false;
    			}
    		}while(stopLoop==false);
    	}
    }
    but i am going to search why your program is wrong.. :P



    superhaNds you got me! :P xD

    last edit :

    here is what i found on the web:

    As per the javadoc for Scanner:

    When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.

    That means that if the next token is not an int, it throws the InputMismatchException, but the token stays there. So on the next iteration of the loop, reader.nextInt() reads the same token again and throws the exception again. What you need is to use it up. Add a reader.next() inside your catch to consume the token, which is invalid and needs to be discarded.

    in your situation
    Java Code:
    catch (InputMismatchException e) {
        System.out.println("-- Please Enter Integer Data --");
        scanner.next(); // this consumes the invalid token
    }
    Thanks :)) , Haha , i learned something new again :>
    Easy + Good explanation

Posting Permissions

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