You are getting StringIndexOutOfBoundsException
because first thing in your
while (number.charAt(0) != '0' || number.length() < 4 || !number.matches("^[0-9]*") || number.isEmpty())
condition is number.charAt(0)
which can throw exception if string is empty ""
. To get rid of this problem move number.isEmpty()
before number.charAt(0) != '0'
.
This is to validate a phone number that starts with 0, and has a minimum length of 4 numbers.
Try this way
while (!number.matches("0[0-9]{3,}"))
3
solved String is empty [closed]