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 6 of 6

Thread: How to validate user input in a loop

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default How to validate user input in a loop

    Is there a method that allows me to check if the user entered anything other than an integer. For example I want the user only to enter an integer, I have this code.

     
    			try 
    			{
    		         integerVariable = sc.nextInt();
     
    			} 
    			catch(InputMismatchException e)
    			{
    		     System.err.println("try again, wrong type");
    			}
    		}

    That is only good for one time. How would I go about doing this in a loop say 5 times or something?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to validate user input in a loop

    Have you tried putting it in a loop?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: How to validate user input in a loop

    Yes I tried a while loop like this, but whenever I enter in a double for example. It will go into an infinite loop saying "try again wrong type"

    int i=0;
    while(i<3)
    		{
    		try 
    			{
    		         integerVariable = sc.nextInt();
                             i++;
    			} 
    			catch(InputMismatchException e)
    			{
    		        System.err.println("try again, wrong type");
    			}
    		}
    		}

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to validate user input in a loop

    If the sc.nextInt() function throws an Exception (which it will if you enter a double), then the i++ line will not be executed.

    Step through this with a debugger, or at least add some print statements, to see what I'm talking about.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: How to validate user input in a loop

    Right, but I thought that it would then eventually reach the top of the while loop and then go back into the try statement.

    I ended up adding sc.next(); in the catch statement and now all is working well.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to validate user input in a loop

    Ah, I understand what you meant now.

    The problem is this: nextInt() reads the next integer, which are separated by white space. The enter key counts as white space, so it isn't read in when you call nextInt(). To move the Scanner past the enter character, you have to call next() or nextLine().
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. The Following User Says Thank You to KevinWorkman For This Useful Post:

    camel-man (October 8th, 2013)

Similar Threads

  1. User Input help
    By lanmonster in forum What's Wrong With My Code?
    Replies: 20
    Last Post: January 20th, 2013, 10:44 AM
  2. How do I use try-catch to validate input?
    By BiaxialPainter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 15th, 2012, 10:59 AM
  3. User Input with a Do Loop
    By RadiantChaos in forum Loops & Control Statements
    Replies: 4
    Last Post: March 13th, 2012, 07:14 PM
  4. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM
  5. Replies: 1
    Last Post: July 28th, 2009, 02:15 AM