Results 1 to 7 of 7
  1. #1
    Kartright is offline Member
    Join Date
    May 2013
    Posts
    20
    Rep Power
    0

    Default InputMismatchException

    Hey,

    I made a quick PIN-Generating program. I think it's solid, but I'm bothered by this:
    Java Code:
    Exception in thread "main" java.util.InputMismatchException
    	at java.util.Scanner.throwFor(Scanner.java:909)
    	at java.util.Scanner.next(Scanner.java:1530)
    	at java.util.Scanner.nextInt(Scanner.java:2160)
    	at java.util.Scanner.nextInt(Scanner.java:2119)
    	at PINGen.main(PINGen.java:12)
    When I enter anything that's not a number.

    Would anyone have any insight as to how to catch the InputMismathException, and instead display it as a println saying whatever I want?

    Here's the code:
    Java Code:
    import java.util.InputMismatchException;
    import java.util.Random;
    import java.util.Scanner;
    
    
    public class PINGen {
    	public static void main(String args[]) {
    		int input, r1, r2, r3, r4, r5, r6, r7, r8, r9;
    		Random gen = new Random();
    		Scanner in = new Scanner(System.in);
            System.out.println("How many digits do you want in your PIN? (Min 3, Max 9)");
            input = in.nextInt();
    		if (input == 3) {
    			r1 = gen.nextInt(9);
    			r2 = gen.nextInt(9);
    			r3 = gen.nextInt(9);
    			System.out.println("Here's your randomly generated 3-digit PIN: " + r1 + "-" + r2 + "-" + r3);
    		} else if (input == 4) {
    			r1 = gen.nextInt(9);
    			r2 = gen.nextInt(9);
    			r3 = gen.nextInt(9);
    			r4 = gen.nextInt(9);
    			System.out.println("Here's your randomly generated 4-digit PIN: " + r1 + "-" + r2 + "-" + r3 + "-" + r4);
    		} else if (input == 5) {
    			r1 = gen.nextInt(9);
    			r2 = gen.nextInt(9);
    			r3 = gen.nextInt(9);
    			r4 = gen.nextInt(9);
    			r5 = gen.nextInt(9);
    			System.out.println("Here's your randomly generated 5-digit PIN: " + r1 + "-" + r2 + "-" + r3 + "-" + r4 + "-" + r5);
    		} else if (input == 6) {
    			r1 = gen.nextInt(9);
    			r2 = gen.nextInt(9);
    			r3 = gen.nextInt(9);
    			r4 = gen.nextInt(9);
    			r5 = gen.nextInt(9);
    			r6 = gen.nextInt(9);
    			System.out.println("Here's your randomly generated 6-digit PIN: " + r1 + "-" + r2 + "-" + r3 + "-" + r4 + "-" + r5 + "-" + r6);
    		} else if (input == 7) {
    			r1 = gen.nextInt(9);
    			r2 = gen.nextInt(9);
    			r3 = gen.nextInt(9);
    			r4 = gen.nextInt(9);
    			r5 = gen.nextInt(9);
    			r6 = gen.nextInt(9);
    			r7 = gen.nextInt(9);
    			System.out.println("Here's your randomly generated 7-digit PIN: " + r1 + "-" + r2 + "-" + r3 + "-" + r4 + "-" + r5 + "-" + r6 + "-" + r7);
    		} else if (input == 8) {
    			r1 = gen.nextInt(9);
    			r2 = gen.nextInt(9);
    			r3 = gen.nextInt(9);
    			r4 = gen.nextInt(9);
    			r5 = gen.nextInt(9);
    			r6 = gen.nextInt(9);
    			r7 = gen.nextInt(9);
    			r8 = gen.nextInt(9);
    			System.out.println("Here's your randomly generated 8-digit PIN: " + r1 + "-" + r2 + "-" + r3 + "-" + r4 + "-" + r5 + "-" + r6 + "-" + r7 + "-" + r8);
    		} else if (input == 9) {
    			r1 = gen.nextInt(9);
    			r2 = gen.nextInt(9);
    			r3 = gen.nextInt(9);
    			r4 = gen.nextInt(9);
    			r5 = gen.nextInt(9);
    			r6 = gen.nextInt(9);
    			r7 = gen.nextInt(9);
    			r8 = gen.nextInt(9);
    			r9 = gen.nextInt(9);
    			System.out.println("Here's your randomly generated 9-digit PIN: " + r1 + "-" + r2 + "-" + r3 + "-" + r4 + "-" + r5 + "-" + r6 + "-" + r7 + "-" + r8 + "-" + r9);
    		} else if (input > 9) {
    			System.err.println("No larger than 9 digit PINS are generated with this program.");
    		} else if (input < 3 && input > 0) {
    			System.err.println("No smaller than 3 digit PINS are generated with this program.");
    		} else if (input == 0) {
    			System.err.println("Your PIN cannot be 0 digits.");
    		} else if (input < 0) {
    			System.err.println("Your PIN cannot contain a negative amount of digits.");
    		}
    		in.close();
    	}
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default Re: InputMismatchException

    Use the hasNext methods to check if valid input exists before attempting to read it.

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

    Default Re: InputMismatchException

    InputMismatchException is thrown by Scanner from nextInt(), if one does not provide an int.
    What you can also do, it place all the code in try-catch block and catch the exception and do what you wish in that unusual situation of bad input.

    Java Code:
    try {
     input = in.nextInt()
     // whatever you are doing
     } catch (InputMismatchException ex) {
        // handle it
     }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default Re: InputMismatchException

    I also think that those long, repeating 'n-digit PIN' blocks deserve a simple loop so those blocks virtually disappear ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Kartright is offline Member
    Join Date
    May 2013
    Posts
    20
    Rep Power
    0

    Default Re: InputMismatchException

    Quote Originally Posted by superhaNds View Post
    InputMismatchException is thrown by Scanner from nextInt(), if one does not provide an int.
    What you can also do, it place all the code in try-catch block and catch the exception and do what you wish in that unusual situation of bad input.

    Java Code:
    try {
     input = in.nextInt()
     // whatever you are doing
     } catch (InputMismatchException ex) {
        // handle it
     }
    Thanks! I had tried the try block before, but I didn't know I had to put what I wanted to do inside of it. I thought I could just put input = in.nextInt() and catch the error.
    Here's my updated code :)
    Java Code:
    import java.util.InputMismatchException;
    import java.util.Random;
    import java.util.Scanner;
    
    
    public class PINGen {
    	public static void main(String args[]) {
    		int input, r1, r2, r3, r4, r5, r6, r7, r8, r9;
    		Random gen = new Random();
    		Scanner in = new Scanner(System.in);
            System.out.println("How many digits do you want in your PIN? (Min 3, Max 9)");
            try {
            	input = in.nextInt();
        		if (input == 3) {
        			r1 = gen.nextInt(9);
        			r2 = gen.nextInt(9);
        			r3 = gen.nextInt(9);
        			System.out.println("Here's your randomly generated 3-digit PIN: " + r1 + "-" + r2 + "-" + r3);
        		} else if (input == 4) {
        			r1 = gen.nextInt(9);
        			r2 = gen.nextInt(9);
        			r3 = gen.nextInt(9);
        			r4 = gen.nextInt(9);
        			System.out.println("Here's your randomly generated 4-digit PIN: " + r1 + "-" + r2 + "-" + r3 + "-" + r4);
        		} else if (input == 5) {
        			r1 = gen.nextInt(9);
        			r2 = gen.nextInt(9);
        			r3 = gen.nextInt(9);
        			r4 = gen.nextInt(9);
        			r5 = gen.nextInt(9);
        			System.out.println("Here's your randomly generated 5-digit PIN: " + r1 + "-" + r2 + "-" + r3 + "-" + r4 + "-" + r5);
        		} else if (input == 6) {
        			r1 = gen.nextInt(9);
        			r2 = gen.nextInt(9);
        			r3 = gen.nextInt(9);
        			r4 = gen.nextInt(9);
        			r5 = gen.nextInt(9);
        			r6 = gen.nextInt(9);
        			System.out.println("Here's your randomly generated 6-digit PIN: " + r1 + "-" + r2 + "-" + r3 + "-" + r4 + "-" + r5 + "-" + r6);
        		} else if (input == 7) {
        			r1 = gen.nextInt(9);
        			r2 = gen.nextInt(9);
        			r3 = gen.nextInt(9);
        			r4 = gen.nextInt(9);
        			r5 = gen.nextInt(9);
        			r6 = gen.nextInt(9);
        			r7 = gen.nextInt(9);
        			System.out.println("Here's your randomly generated 7-digit PIN: " + r1 + "-" + r2 + "-" + r3 + "-" + r4 + "-" + r5 + "-" + r6 + "-" + r7);
        		} else if (input == 8) {
        			r1 = gen.nextInt(9);
        			r2 = gen.nextInt(9);
        			r3 = gen.nextInt(9);
        			r4 = gen.nextInt(9);
        			r5 = gen.nextInt(9);
        			r6 = gen.nextInt(9);
        			r7 = gen.nextInt(9);
        			r8 = gen.nextInt(9);
        			System.out.println("Here's your randomly generated 8-digit PIN: " + r1 + "-" + r2 + "-" + r3 + "-" + r4 + "-" + r5 + "-" + r6 + "-" + r7 + "-" + r8);
        		} else if (input == 9) {
        			r1 = gen.nextInt(9);
        			r2 = gen.nextInt(9);
        			r3 = gen.nextInt(9);
        			r4 = gen.nextInt(9);
        			r5 = gen.nextInt(9);
        			r6 = gen.nextInt(9);
        			r7 = gen.nextInt(9);
        			r8 = gen.nextInt(9);
        			r9 = gen.nextInt(9);
        			System.out.println("Here's your randomly generated 9-digit PIN: " + r1 + "-" + r2 + "-" + r3 + "-" + r4 + "-" + r5 + "-" + r6 + "-" + r7 + "-" + r8 + "-" + r9);
        		} else if (input > 9) {
        			System.err.println("No larger than 9 digit PINS are generated with this program.");
        		} else if (input < 3 && input > 0) {
        			System.err.println("No smaller than 3 digit PINS are generated with this program.");
        		} else if (input == 0) {
        			System.err.println("Your PIN cannot be 0 digits.");
        		} else if (input < 0) {
        			System.err.println("Your PIN cannot contain a negative amount of digits.");
        		}
            } catch (InputMismatchException ex) {
            	System.err.println("You need to input an integer!");
            }
    		in.close();
    	}
    }
    And to JoaSH, I'm not quite sure how loops work.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default Re: InputMismatchException

    Quote Originally Posted by Kartright View Post
    And to JoaSH, I'm not quite sure how loops work.
    You'd be happy then that you don't have to generate a, say, 100,000 digit PIN code. If you don't know something Java related, read the tutorials; it's all in there ...

    kind regards,

    Jos
    Last edited by JosAH; 06-04-2013 at 04:13 PM. Reason: silly typo ...
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Kartright is offline Member
    Join Date
    May 2013
    Posts
    20
    Rep Power
    0

Similar Threads

  1. InputMismatchException Error
    By fatihkok in forum New To Java
    Replies: 3
    Last Post: 02-04-2013, 03:41 PM
  2. throw new InputMismatchException
    By Pat in forum New To Java
    Replies: 8
    Last Post: 10-09-2012, 09:53 AM
  3. InputMismatchException
    By gnbradford in forum New To Java
    Replies: 3
    Last Post: 09-22-2012, 09:45 PM
  4. InputMismatchException
    By jihad in forum New To Java
    Replies: 5
    Last Post: 12-17-2011, 03:10 PM
  5. try-catch InputMismatchException in a while loop
    By themulator in forum New To Java
    Replies: 17
    Last Post: 10-12-2010, 04:49 AM

Posting Permissions

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