There are some errors in your code.
Line 9 has two errors:
You should use
== rather than
= to compare characters. It won't even compile with a single
=. Also, the
; semi-colon must not be there. The semi-colon is an empty statement. What the line basically says now is: assign
letter to
desiredChar and if that is
true, do nothing.
Assuming this would compile, the block in lines 10 - 14 will always be executed, regardless of the expression in the if-statement. Remove the semi-colon.
You're on the right track. It is not necessary to convert the string to a char array to find a character - using
charAt is fine. If you want to find the last occurrence of a character in a string, it would however be more efficient to start searching from the end of the string and go backwards, instead of starting at the beginning. Then you can stop searching as soon as you find the desired character.