I'm having a bit of trouble trying to figure out how to prevent user input that are numerical. I understand how to prevent non-numerical inputs (ie. inputting a letter instead of a number) but not the other way around. How do I work around this?
String[] player_Name = new String[game];
for (i = 0; i < game; i++) {
try {
player_Name[i] = JOptionPane.showInputDialog("Enter the name of the
player, one by one. ");
} catch(Exception e) {
JOptionPane.showMessageDialog(null, "Enter a valid name!");
i--;
}
Use a do/while statement. "Do input while the input contains at last one number".
String[] player_Name = new String[game];
for (int i = 0; i < game; i++) {
String input;
do {
input = JOptionPane.showInputDialog("Enter the name of the
player, one by one. ");
} while (input.matches(".*\\d+.*"));
player_Name[i] = input;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With