I am trying to create a string of six alphanumeric characters. The code below works fine most of the time but on the rare occasion a string of six alpha characters gets through, how can I ensure that the returned string is always alphanumeric?
String code = "";
while(!code.matches("[a-zA-Z0-9]+$"))
{
code = Integer.toString((int) (Math.random() * Integer.MAX_VALUE), 36);
}
return code;
Repeated attempts is possibly the simplest, but you have to check it contains a letter and a digit.