Trying to verify that a three character sting is in a specified format

40 views Asked by At

My result after passing the value "A2A" - which should return false I want to check that a three character string matches the format 12A. The first two must be a digit and the 3rd must be a letter. The string "A2A" must therefore return false. What is happening here and how stupid am I being?

I've tried various ways of doing this and nothing seems to work.

1

There are 1 answers

0
Tim Biegeleisen On

Just use String#matches() here with a regular expression:

public boolean validate(String input) {
    return input.matches("[0-9]{2}[A-Za-z]");
}