Results 1 to 3 of 3
  1. #1
    Galway is offline Member
    Join Date
    Sep 2016
    Rep Power

    Question I am having trouble with a try catch (statement?) within a loop.

    Java Code:
    import java.util.Scanner;
    public class NameAge {
    	public static void main(String[]args){
    		String name;
    		int age=0;
    		boolean y=true;
    		Scanner input = new Scanner(;
    		System.out.print("Enter your name: ");;
    	while (y){
    			System.out.print("Enter your age: ");
    		}catch(java.util.InputMismatchException e){
    			System.out.println("Enter integer for age");
    		System.out.println("Hi " + name + " you are " + age + " years old.");
    Here is the program it works without the try catch but I want to learn what is going wrong here. It just keeps printing out
    "Enter your age: Enter integer for age" endlessly. This confuses me in two ways. First why is it printing both on the same line when both are println statements?
    2nd why is the loops going endlessly and not allowing the used to enter age again?
    Last edited by Tolls; 09-29-2016 at 05:26 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: I am having trouble with a try catch (statement?) within a loop.

    The previous garbage is still in the buffer so you need to clear it, otherwise you will loop forever.
    Just before your prompt within the loop, do a input.nextLine(). This works because there is also
    a new line from the first prompt of the name.

    Last edited by jim829; 09-29-2016 at 05:36 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: I am having trouble with a try catch (statement?) within a loop.

    Can you show what you entered for a name?

    Because if you entered anything with a space in it you would get the exception thrown, which would then put you in a permanent loop.

    The reason is, if you enter:
    john smith
    the call to next() will read in 'john' as the default separator for Scanner is any whitespace (so space, newline, tab etc).
    This will leave 'smith' in the buffer.

    The code then asks for age and a call to nextInt is made.
    'Smith' is in the buffer, so an exception is thrown.

    The exception handler prints its message and the loop goes round again.
    nextInt is called.
    'Smith' is still in the buffer (Scanner won't consume it on an exception)...and round we go.

    The first thing is to possibly use nextLine instead of next() in the first case (for name).
    After that you probably also want a throw away call like that in the catch block.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. try catch statement and scanner variable
    By jwl in forum New To Java
    Replies: 3
    Last Post: 09-17-2012, 01:31 AM
  2. catch error statement
    By droidus in forum New To Java
    Replies: 11
    Last Post: 03-16-2012, 09:49 AM
  3. try/catch and if statement conundrum
    By thatguywhoprograms in forum New To Java
    Replies: 4
    Last Post: 12-19-2011, 10:20 PM
  4. Replies: 2
    Last Post: 02-19-2011, 06:40 AM
  5. Trouble with Try Catch blocks and file reading.
    By theBurgh22 in forum New To Java
    Replies: 2
    Last Post: 11-30-2010, 01:11 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