Skip to content

Instantly share code, notes, and snippets.

@dhust
Last active December 20, 2015 12:58
Show Gist options
  • Save dhust/6134689 to your computer and use it in GitHub Desktop.
Save dhust/6134689 to your computer and use it in GitHub Desktop.
IO - validation
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number = 0;
Scanner extraction;
while (number < 1 || number > 10) {
System.out.print("Enter a number (1-10): ");
String line = input.nextLine();
extraction = new Scanner(line);
if (extraction.hasNextInt()) {
number = extraction.nextInt();
}
}
System.out.println("Your number was: " + number);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment