I have a question about validation in Java, I have looked at previous topic and none seem to answer my little problem.
What I am trying to do is validate what is put into a string variable in the constructor but also when a set method is used.
What I need to is If Mr Miss Mrs or Ms is entered, then to set that in a Variable (title) if not then to set title as ”Not set” and print an error, now I know how to the the last part, its the the validation of what the user is entering into the variable I am stuck in… I have tried using an array but couldn’t get it to work and tried if statements, again couldn’t get it to function
if you want to do case insensitive then change the regular expression to:
then you can just lowercase the entire thing and uppercase just the first letter to re-normalize your input to what you really want. Google for “java proper case” to find some pre-written snippets of code.
A more concise one liner, again case sensitive: