aspose file tools*
The moose likes Beginning Java and the fly likes manipulating a char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "manipulating a char" Watch "manipulating a char" New topic
Author

manipulating a char

Lara Fabans
Greenhorn

Joined: Jun 11, 2003
Posts: 2
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

Joined: Jan 30, 2002
Posts: 3451
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?


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Lara Fabans
Greenhorn

Joined: Jun 11, 2003
Posts: 2
>> 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
 
 
subject: manipulating a char