3

I have created the following class for Inputting a user's age and then displaying appropriate info in the console.

On running this program , the console asks "Please Enter your Age : "

If the user enters an Integer for eg: 25 , the executed class displays " Your age is : 25" in the console.

If the user enters a non-integer number , the console displays: Age should be an Integer Please Enter your Age:

But I am not able to enter anything through the keyboard when I place my cursor next to "Please Enter your Age: ".

I want the user to be able to enter his age again, & if he enters an integer it displays the proper output but if he enters a non-integer the console should ask him again for the age.

If you look at my code I'm setting the value of the variable 'age' by calling the function checkAge() inside the else block in my main function.

Can anybody tell me where I am going wrong?

public class ExceptionHandling{

    static Scanner userinput = new Scanner(System.in);

    public static void main(String[] args){ 
        int age = checkAge();

        if (age != 0){
            System.out.println("Your age is : " + age);         
        }else{
           System.out.println("Age should be an integer");
           age = checkAge();
        }
    }

    public static int checkAge(){
        try{            
            System.out.print("Please Enter Your Age :");
            return userinput.nextInt();
        }catch(InputMismatchException e){
            return 0;
        }
    }
}
GameDroids
  • 5,584
  • 6
  • 40
  • 59
Anshuman Tripathy
  • 113
  • 1
  • 3
  • 12

2 Answers2

3

You should put your code in a loop if you wish it to execute multiple times (until the user inputs a valid age) :

public static void main(String[] args)
{
    int age = checkAge();
    while (age == 0) {
       System.out.println("Age should be an integer");
       userinput.nextLine();
       age = checkAge();
    }

    System.out.println("Your age is : " + age);
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Nope...I already tried that. If I put it in a loop, the console goes wild. It keeps asking for age until I stop the console. Also if I put in your code it takes the value of age as 0, no matter what I have inputted initially. i.e. if I input 5.89...this line keeps getting repeated in the console: Please Enter Your Age :Your Age is : 0 – Anshuman Tripathy Jul 28 '14 at 19:37
  • @AnshumanTripathy Rod is right about adding `userinput.nextLine();`, but you still need to use a loop. Updated answer. – Eran Jul 28 '14 at 19:44
  • ya I messed around with the code and this one works: public static void main(String[] args){ int age = checkAge(); while (age == 0){ System.out.print("Age should be an Integer"); userinput.nextLine(); age = checkAge(); } System.out.println("Your Age is : "+ age); Thanks for your help :) – Anshuman Tripathy Jul 28 '14 at 19:58
2

problem:

return userinput.nextInt();

By the time you input a sequence of string it will not consume your new line character, and when you go again inside your method and call userinput.nextInt() it will consume that new line and skip it thus not letting you get input again.

solution:

add nextLine(); before you call the checkAge method again to consume the new line from the string

sample:

    System.out.println("Age should be an integer");
    userinput.nextLine();
    age = checkAge();
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • On doing this change, I am able to input a value for "Please Enter Your Age:" but on giving a value the console goes blank. as though its not even calling the checkAge() function. eg: case 1: Please Enter Your Age :8.7 Age should be an integer Please Enter Your Age :9.9 //BLANK CONSOLE case 2: Please Enter Your Age :8.7 Age should be an integer Please Enter Your Age :90 //BLANK CONSOLE – Anshuman Tripathy Jul 28 '14 at 19:42