Мне пришлось сделать калькулятор графического интерфейса (работает только с целыми числами), и проблема была в том, что
Тесты не позволяли создавать исключения, если входные данные не были
Integer. Поэтому я не мог использовать
try { int x = Integer.parseInt(input)} catch (Exception e) {dosomethingelse}
Поскольку программы Java обычно обрабатывают ввод в JTextField как строку
Я использовал это:
if (input.matches("[1-9][0-9]*"){ // String.matches() returns boolean
goodforyou
} else {
dosomethingelse
}
// this checks if the input's (type String) character sequence matches
// the given parameter. The [1-9] means that the first char is a Digit
// between 1 and 9 (because the input should be an Integer can't be 0)
// the * after [0-9] means that after the first char there can be 0 - infinity
// characters between digits 0-9
надеюсь, это поможет:)