ReaperLPN
ReaperLPN

Reputation: 1

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

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.

Upvotes: -1

Views: 49

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

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

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

Upvotes: 0

Related Questions