Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Validating empty string

 
Nico Yukiko
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i wrote if (email==""){
System.out.println("error")
}
but it does not show this is i leave emai as empty???
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should always compare Strings using the equals method. == will not always return true.

Mark
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The == operator here tests whether "" and email point are in the same memory location. Even if the variable email did in fact contain an empty string, the == doesn't know that's what you mean. The equals() method in the String class is a test for what's in the String, rather than where the thing is located.
 
Stephen Boston
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comparing Strings was difficult for me as well. I think this is what you are looking for.



[ September 13, 2005: Message edited by: Stephen Boston ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic