Created
April 21, 2019 23:23
-
-
Save wseemann/636429201e04d106cb2fea9fe4ea52d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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