27 lines
704 B
Java
27 lines
704 B
Java
package labbar;
|
|
|
|
import javax.swing.JOptionPane;
|
|
|
|
|
|
public class UserInterface {
|
|
public static void printMessage(String message) {
|
|
JOptionPane.showMessageDialog(null, message);
|
|
}
|
|
|
|
public static int askForInt(String message) {
|
|
String input = JOptionPane.showInputDialog(message);
|
|
if (input == null) return -2;
|
|
|
|
// Shadow input
|
|
input = input.chars()
|
|
.filter(Character::isDigit)
|
|
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
|
|
.toString();
|
|
|
|
try {
|
|
return Integer.parseInt(input);
|
|
} catch (NumberFormatException e) {
|
|
return -1;
|
|
}
|
|
}
|
|
}
|