Input Validation via Do While Loop
import java.util.Scanner; public class InputValidationDoWhile { public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); int number;/* ww w . j ava 2 s .c om*/ boolean firstRun = true; // Get and validate the input. do { if (!firstRun) {// first time still true so basically saying that if not first run can print invalid msg. System.out.println("That number is invalid."); } System.out.print("Enter a number in the " + "range of 1 through 100: "); number = keyboard.nextInt(); firstRun = false; // this makes false so from now on will be false and can now print invalid message } while (number < 1 || number > 100); keyboard.close(); } }