It's not a secret anymore!*
The moose likes Beginning Java and the fly likes String Comparison ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Comparison ?" Watch "String Comparison ?" New topic
Author

String Comparison ?

Madhu Iyer
Greenhorn

Joined: Dec 27, 2005
Posts: 4
Can someonew tellme how to go about srting comparison.

I have the input from the System as b.readLine() and I want to compareit with String "Exit".

if i write
if(b.readLine()=="Exit"){
// My code goes here
}

even after typing Exit it does not enter the IF Loop...........

HELP !!!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Don't use "==" to compare Strings; use the "equals()" method.

if (myString.equals("Exit")) ...

"==" asks if two objects are the same physical object in memory, while "equals()" asks if they are interchangeable or equivalent, even if they are distinct objects.


[Jess in Action][AskingGoodQuestions]
madhup narain
Ranch Hand

Joined: Dec 14, 2004
Posts: 148
Welcome to the real world of java.


Money for nothing and Java for Free
SCJP, SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Comparison ?
 
Similar Threads
Conditions on field
how to read integers from keyboard?
about this array class
strreams 4 massive file?
problem with try catch