my dog learned polymorphism*
The moose likes Beginning Java and the fly likes It seems so simple, Help ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "It seems so simple, Help !" Watch "It seems so simple, Help !" New topic
Author

It seems so simple, Help !

Albert Kam
Ranch Hand

Joined: Oct 18, 2007
Posts: 58
Dear all,

I think this simple lines of code already says the intention of it ..


And the output is -1

Why is it not located by the indexOf method ? Any workaround for this ?
Please share your thoughts ..

Regards,
Albert Kam


Be nice !
Albert Kam
Ranch Hand

Joined: Oct 18, 2007
Posts: 58
Dear all,

Actually i just want to add a byte to string, and later on, find and replace the byte with a unicode char, and i ended up the first question.

Anyway i found the solution .. here's the code that works :
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

String's indexOf is overridden to take a char. So if you're providing a byte, the byte is converted to char. Interestingly, this is done by first widening to type int and then narrowing to type char. (See JLS 5.1.4.)

So s.indexOf((byte)218) is looking for the char represented by 65498, but I assume you want it to look for a String representation of the byte value -38. That is, s.indexOf(Byte.toString((byte)218)).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Albert Kam
Ranch Hand

Joined: Oct 18, 2007
Posts: 58
Thank you bartender
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: It seems so simple, Help !