• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

if (String == String)

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello...
I'm having troubles comparing strings in an if statement...
I have a file that I have read (line by line) and then tokenized with StringTokenizer.
Now, the second token of every line is either the letter P or S (a sales spreadsheet, P stands for produced, and S for sales).
But, when I take the token and assign it to a String variable I cannot compare it to a P or S in an If statement.
transtype = str.nextToken();
if (transtype == "P"){...
Now... I know that token is A)a string, and B)the letter P (capitalized, if it matters).
However, when the program runs it completely ignores the If statement, as if the if failed.
Why can't I compare them...
Thanks for any help!
-Andrew
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you compare objects with "==" the reference address is compared
not the content. If you want to campare the content of the string
use:
"P".equals(yourString)
Then it should work as expected
 
Andrew M.
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simple and effective...
Thank you so much...
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andrew M.,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
An initial isn't enough for a dislayed last name.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose you've 2 strings, "Hello World" & "hello world", do you consider them to be the same?

If you do, the equals() method won't quite work. Use the equalsIgnoreCase() method instead.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic