This regex should match your need (with leading zeros):
[0-9]{5}
and you will use a while loop, looping until these two conditions are met, something like
while (!inputString.matches("[0-9]{5}")) {
// ask again and again
if (!isInteger(inputString)) {
// invalid input
} else {
if (inputString.length() < 5) {
// too low
} else if (inputString.length() > 5) {
// too high
}
}
}
And you can use a helper method like this:
public boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch(NumberFormatException e) {
return false;
}
return true;
}