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
-
String title = textBoxTitle.getText(); if(!title.equals("Mr") && !title.equals("Miss") && !title.equals("Mrs") && !title.equals("Ms")) { // Text is invalid } else { // Text is valid }fuzzy lollipop : negative tests are hard to read and understand from a maintence standpoint, why not test of equals || equals || that would be better than a negative test like this. -
why not just call a small method that does the validation and then sets the value?
private void validateString(String) { if string is valid call setter }Anon : or turn this into `validateAndSet()` and call from both ctor and public setterfuzzy lollipop : a method name should not have a side affect that isn't expected, this is a terrible name. methods should do one thing and one thing only, the internal workings should be a black box, there is absolutely no reason to have "validate" in the name of the method. All the caller should be concerned with is the end result. if you were going to have it called `isValidPrefix` that then returned a true or false that would be better, as it stands this is horrible. -
public void setTitle(final String title) { if (title.matches("^Mrs|Mr|Miss|Ms$")) { this.title = title; } else { this.title = "Not Set"; System.err.format("%s is not a valid title, expecting Mrs,Mr,Miss,Ms\n", title); } }if you want to do case insensitive then change the regular expression to:
"(?i)^Mrs|Mr|Miss|Ms$"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:
public void setTitle(final String title) { title.matches("^Mrs|Mr|Miss|Ms$")) ? this.title= title: this.title= "Not Set"; }Necronet : I like this answer better... is quite flexible with Regexp! -
i'm pretty sure u did something like --
if(title =="Mr."){ blah.. } do this instead .. if(title.equals("Mr."){blah..}Chris : This is what I did do it was when shown the ''title.equals'' I realised what I was doing wrong. -
Create a set of the words that you care about (capitalize them), and then see if the title falls into the set:
Set allowedTitles = new HashSet(); allowedTitles.add("MR"); allowedTitles.add("MRS"); allowedTitles.add("MS"); allowedTitles.add("MISS"); if (! allowedTitles.contains(textBoxTitle.getText().trim().toUpperCase()) { //print exception } else { title = textBoxTitle.getText().trim());fuzzy lollipop : checking against capitalization is a bad idea because of unicode, capital letters in unicode can be problematic, lowercase on the other had is normalized to the same characters. also the original question wants them in Proper Case, they don't specify anything about case insensitivity.Matthew Flynn : Fair enough. Use toLowerCase() then. I think the downvote is a little harsh, as the concept was to use "contains". If case isn't an issue, then don't bother with it, obviously.
0 comments:
Post a Comment