Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

manipulating a char

 
Lara Fabans
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got
String bar = "SDRTRed"; //well, not really, it's one of a number of different strings like this being passed in
char foo = bar.charAt(3);
Then I want to say
if (foo == 'R')
{
System.out.println("I found Reading");
}
But it the compiler keeps telling me: operator == cannot be applied to char, java.lang.String
Ok, I can figure out what that means :-) So I tried if (foo.equals('R')), but it says I can't do that either.
So, if you search in the java doc stuff for char and equals, you get more hits then you can shake a stick at.
Do I really need to use the substring() method of the String to determine if it .equals() what I'm looking for?
Thanks
Lara
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch Lara.
Umm, I don't see anything wrong with the code. I just compiled and ran this:

And it works fine. Are you sure that you are using single quotes in the if statement?
 
Lara Fabans
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>> Welcome to JavaRanch Lara.
Thanks! I hope I can give something in return soon.
>> Umm, I don't see anything wrong with the code. I just compiled and ran this:
Darn :-)
>> And it works fine. Are you sure that you are using single quotes in the if statement
Yuppers:
char testSub = testId.charAt(2);
if (testSub == 'R')
{

It's webObjects 5.2 using jdk1.3.1
If it's *supposed* to work that way, I'll try again. Maybe I've got a secret handshake character in there somewhere.
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic