Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2007

Tip: Looking for answers? Try searching our database.

Problem using a string in an if statement

Thread view: 
chps - 03 Jan 2007 21:40 GMT
Hello all

I have Class File where I read information in from a text file and split it
into sections using string.split(",").

The elements are then allocated to a string array and then to variables

[code]

String[ ] infoarray = info.split(",");

string0 = infoarray[0];

string1 = infoarray[1]; //etc

[end code]

The variables can be accessed by other Classes using get() methods.

[code]

public String getYards(){

                       return string1; //willl be either "yds" or "m"

}

[end code]

I then need to use the information in an if statement where ri is an
instance of the file reading class

[code]

If(ri.getYards() == "yds"){

           switchflag = 1;

}

else{ switchflag = 0;

}

[end code]

If I print out ri.getYards() using System.out.println() it returns yds but
the result of the if  statement above is false rather than true and the
integer variable switchflag is 0.

I expect I'm missing something very obvious but would appreciate any help.
The books I have shed no light on the problem.

Thank you for your time

Stuart
Andrew Thompson - 03 Jan 2007 21:51 GMT
...
> If(ri.getYards() == "yds"){

'If'?  'If' would not compile.  Please post actual codes,
(copy/paste) rather than this pseudo-code (rubbish),
in future.

But to the question..

String comparison should be done on the contents
of the strings, rather than by comparing their
references (which is what happens above).

To compare the contents, change it to..

 if( ri.getYards().equals("yds") ) {

..or better..

 if ( ri.getYards()!=null &&
   ri.getYards().equals("yds") ) {

Andrew T.
Lew - 03 Jan 2007 23:08 GMT
> To compare the contents, change it to..
>
[quoted text clipped - 4 lines]
>   if ( ri.getYards()!=null &&
>     ri.getYards().equals("yds") ) {

Also, two points about the data structure:

"string0" is a bad name on two counts: it is really just another way to say
"infoarray [0]" (should be "infoArray", or better, "info"), and calling
something "stringWhatever" or "whateverArray" embeds implementation in the
name, violating the principle of information hiding and giving absolutely no
clue as to the purpose of the variable. What is the self-documenting essential
connection between the fact that something is a String, at position 1, and
whether the contents represent the choice of yards or meters?

An array is supposed to represent a bunch of the same kinds of things,
usually, not many different kinds of things. (Seen from the problem domain,
not the implementation domain.)

The second point is that instead of an array where every element "means"
something different, you should consider creating an object whose fields (with
meaningful names) hold the information:

public class MetaInfo
{
  String yardsOrMeters = "yds"; // better yet, use an enum!
 ...
}

- Lew
Andre Hinrichs - 04 Jan 2007 01:22 GMT
> ...
>> If(ri.getYards() == "yds"){
[quoted text clipped - 17 lines]
>   if ( ri.getYards()!=null &&
>     ri.getYards().equals("yds") ) {

You can omit the null check if you write this:

 if ( "yds".equals(ri.getYards()) ) {
TechBookReport - 04 Jan 2007 10:27 GMT
>> ...
>>> If(ri.getYards() == "yds"){
[quoted text clipped - 20 lines]
>
>   if ( "yds".equals(ri.getYards()) ) {

You're right, but personally I just don't like the way that code looks.
It's odd, but the aesthetics don't seem right to me.

Pan

Signature

TechBookReport Java - http://www.techbookreport.com/JavaIndex.html



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.