File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes String Comparison Problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Comparison Problem." Watch "String Comparison Problem." New topic
Author

String Comparison Problem.

Brett Freedman
Greenhorn

Joined: Feb 26, 2010
Posts: 13

First example: I set posTest to "C", display it and then test it for "C". The if statement works. playbyplayText is "CX".



Second example: I pull the "C" out of my teamplayer object. The "C" displays but the if statement does not fire. playbyplayText is only "C".




Here is my player class. I am reading the data out of a space delimited text file.

Why won't (posTest == "C") evaluate as true?

Sample data:



Declaration and class:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Never use == for String comparison but use the equals method instead. == checks for instance equality whereas the equals method actually checks the String contents.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Brett Freedman
Greenhorn

Joined: Feb 26, 2010
Posts: 13

This is the complete code that builds the Player objects.

Brett Freedman
Greenhorn

Joined: Feb 26, 2010
Posts: 13
Rob Prime wrote:Never use == for String comparison but use the equals method instead. == checks for instance equality whereas the equals method actually checks the String contents.


Fixed. Thanks Rob! I've been going around and around with that bit of code all day.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

You're welcome.
 
GeeCON Prague 2014
 
subject: String Comparison Problem.