Skip to content

Instantly share code, notes, and snippets.

@wseemann
Created April 21, 2019 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wseemann/636429201e04d106cb2fea9fe4ea52d0 to your computer and use it in GitHub Desktop.
Save wseemann/636429201e04d106cb2fea9fe4ea52d0 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class SampleMain {
public static void main(String [] args) {
Scanner scanner = new Scanner(System.in);
// validate input
String validInput = validateInput(scanner);
// valid input
System.out.println("Valid input: " + validInput);
}
private static String validateInput(final Scanner scanner) {
String input = scanner.nextLine();
// if the input contains a letter request new input
while (input.matches(".*[a-zA-Z]+.*")) {
System.out.println("Input string cannot contain letters, please try again...");
input = scanner.nextLine();
}
return input;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment