I'm attempting to test user input for a function without using the try/catch method. I need to test the String input to be both > 0 and also test that it is an integer. Is there any advice on how to implement this?
Thank you.
I'm attempting to test user input for a function without using the try/catch method. I need to test the String input to be both > 0 and also test that it is an integer. Is there any advice on how to implement this?
Thank you.
Use this to check
import java.util.regex.Pattern;
public class NaturalNumberChecker {
public static final Pattern PATTERN = Pattern.compile("[0-9]+");
boolean isNaturalNumber(String input) {
return input != null && PATTERN.matcher(input).matches();
}
}
if wants to handle the case when user may enter integer with spaces use trim method to delete leading and trailing spaces in user input