user6254638 user6254638 - 7 months ago 12
Java Question

Not sure what loop to use in this case

I'm trying to make a simple program that asks for the user's age and displays an error when the user inputs a non-integer value.

Here's what I did so far:

import java.util.Scanner;

public class apples {

public static void main(String args[]) {

Scanner nameinput = new Scanner(System.in);

System.out.println("Please enter your name to begin.");

System.out.println("Hello " + nameinput.nextLine() + "!");

Scanner ageinput = new Scanner(System.in);

System.out.println("Please enter your age");

if(!ageinput.hasNextInt()){

System.out.println("Please enter an integer");

}

System.out.println("You've entered a valid age");

nameinput.close();
ageinput.close();
}

}


Here's what I want:

Every time the user inputs a non integer, I want the
Please enter an integer
error to appear. The user should then be able to input their age again, which will again be checked if it's an integer and so on. This will continue until the user inputs an integer and only then will the message
You've entered a valid age
be shown. I'm sure about neither which loop to use in this case (for, while, do while) nor how to implement it in the code.

Answer

Hope this is what you wanted. i would suggest that using while is the way to go (my preference)
Using while::

public static void main(String args[]) {

    Scanner nameinput = new Scanner(System.in);

    System.out.println("Please enter your name to begin.");

    System.out.println("Hello " + nameinput.nextLine() + "!");

    Scanner ageinput = new Scanner(System.in);

    System.out.println("Please enter your age");

    while (!ageinput.hasNextInt()) {
        System.out.println("Please enter an integer");
        ageinput.next();
    }
    System.out.println("You've entered a valid age");
    nameinput.close();
    ageinput.close();
}

Using for::

public static void main(String args[]) {

    Scanner nameinput = new Scanner(System.in);

    System.out.println("Please enter your name to begin.");

    System.out.println("Hello " + nameinput.nextLine() + "!");

    Scanner ageinput = new Scanner(System.in);

    System.out.println("Please enter your age");

    for (; !ageinput.hasNextInt();) {
        System.out.println("Please enter an integer");
        ageinput.next();
    }
    System.out.println("You've entered a valid age");
    nameinput.close();
    ageinput.close();
}

Using do while

public static void main(String args[]) {

    Scanner nameinput = new Scanner(System.in);

    System.out.println("Please enter your name to begin.");

    System.out.println("Hello " + nameinput.nextLine() + "!");

    Scanner ageinput = new Scanner(System.in);
    int i = 0;
    do {
        if (i == 0) {
            System.out.println("Please enter your age");
            i++;
        } else {
            System.out.println("Please enter an integer");
            ageinput.next();
        }
    } while (!ageinput.hasNextInt());
    System.out.println("You've entered a valid age");
    nameinput.close();
    ageinput.close();
}