This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes String validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String validation" Watch "String validation" New topic

String validation

trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
Hello all,

In my code I have declared a String name;

An I am doing following validations..
if(name == null)
{doSomething; }
else if(name != null){do Somethign;}

What I have observed is when I write if(name != null)condiiton before if(name == null)condiiton, when the name is not null the if(name != null) condiiton is ignored.
but if works fine as mentioned in the first para.

why is this happening?

pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
since a value can be either null or not null there is no need for both conditions:

is how you can validate your strings.

if the conditions are exclusive, then the order is not important.

Ben Ethridge
Ranch Hand

Joined: Jul 28, 2003
Posts: 108
I agree with Pascal, but just for the sake of argument, I wrote a simple one:

Works ok for me. You sure there isn't something else going on (maybe some subtlety) in your code?

Point of TV trivia: Who (or should I say what) used to shout the String I have in the else above?

SCJB 1.4
I agree. Here's the link:
subject: String validation
It's not a secret anymore!