The input given is Integer + String, however I want to set a range of integer input (1~34) and String (A~F).
if I do,
int i = (Integer.parseInt(input));
if (n > 34) {
System.out.println("number too high");
}
it gives me
Exception in thread "main" java.lang.NumberFormatException: For input string: "35A"
is there any way to make this work? thanks in advace 🙁
if the string is 1A to 34F it should pass, anything over that should fail.. please help!
>Solution :
You could first split the String into number and letter
String[] split = n.split("(?=[A-Z])");
then check if the two parts fit your needs
if(Integer.parseInt(split[0]) > 34 || split[1].charAt(0) > 'F') {
System.out.println("number too high");
}
You’d have to check beforehand that your String actually conforms to that pattern but you said in the comments that it’s always in that format, so it should work. Also if lowercase letters are possible you have to change the regex and condition or simply call toUppercase() on the input String.