Q:

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.

A:

As you know in Java they're 3 types of loops:
The while and do-while Statements

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once 

while is simply a pre-test loop, that is the condition first will be checked before moving on to the body of the loop: 
while(condition)       # First check, if true or false
{ 
   # Body
}

do-while loop however, checks the condition after executing of the body of the loop at least once, it's a post-test loop: 
do{    
       # Body executed at least once
 }while(condition);

The for Statement

The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied

Notice that in your code, asking the user for the age, the proper choice would be do-while, because you need to execute your program at least once to prompt the message and then you have to check the condition, if that's what you intended to do then this will suffice for your purpose. Though, you still can use while. 
This is your code edited: 
import java.util.Scanner;

public class apples {

public static void main(String args[]) {

    Scanner input = new Scanner(System.in);
    System.out.println("Please enter your name to begin.");
    System.out.println("Hello " + input.nextLine() + "!");

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

    do{

         if(input.hasNextInt()){
                 System.out.println("You've entered a valid age");
                 break; 
              }

         else{
                System.out.println("Please enter an integer");
                input.next(); 
           }

    }while(true);

  }

}

Since the condition for the loop is the Boolean true it really doesn't matter here if you use while instead of do-while unless if you want to change the condition, that's up to you. For now, this code is very much close to the original code you posted and from my perspective it works, but it may not be the best code out there, there could be other approaches simpler or more complex to the same problem domain.

