wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Is there a way to search for a Characters location in a String without using lastindexOf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is there a way to search for a Characters location in a String without using lastindexOf" Watch "Is there a way to search for a Characters location in a String without using lastindexOf" New topic
Author

Is there a way to search for a Characters location in a String without using lastindexOf

Jim Stevens
Greenhorn

Joined: Jun 08, 2011
Posts: 28
I was wondering if this was possible to find a characters location in a String without using lastIndexOf. I was trying to think of different ways this is possible.
My thoughts so far where creating a for loop to search each individual character in the string. Im just not sure how to print out the location of that character once found...
Any ideas?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Sure, the for-loop should work fine. I don't see why it's a problem to print out the location of the character, given that you must have extracted the character from that same location just a couple of lines of code earlier. But if you have some particular code to discuss, go ahead and post it.
Jim Stevens
Greenhorn

Joined: Jun 08, 2011
Posts: 28

I feel like im on the right track here but when i compare desiredChar = letter in the if statement it always comes back as true and prints pass....
Walter Gabrielsen Iii
Ranch Hand

Joined: Apr 09, 2011
Posts: 158
Why are you trying to avoid the lastIndexOf methods, and by extension I'm also assuming you want to avoid IndexOf methods?

By methods I mean these methods are overloaded to include int fromIndex which in a for loop, knowing the first and last places of a char, and adjusting the search index, could be useful for skipping over large parts of a String and only searching an ever shrinking range.
Jim Stevens
Greenhorn

Joined: Jun 08, 2011
Posts: 28
Just trying to learn more. Figured it would be good review of for loops and if statements. But now i cant figure it out so its just bugging me haha.
Thanks in advance
Walter Gabrielsen Iii
Ranch Hand

Joined: Apr 09, 2011
Posts: 158
If you want to test loops then perhaps the toCharArray() method to return a char array would be useful.
Jim Stevens
Greenhorn

Joined: Jun 08, 2011
Posts: 28
So your saying converting s into an array then using a for loop and if statement to find the location?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You've fallen into one of the many traps which Java sets for beginners.

This says (or it would say if you had used == instead of =) "If desiredChar is equal to letter then do nothing."

That semicolon at the end is an example of a "null statement"; the result of a null statement is that nothing happens. But it's worse than nothing, because the code after that isn't part of what is controlled by the "if", so it is always executed regardless. It's only the null statement which is controlled by the "if".

So get rid of that semicolon. (And have another look at the published examples of if-statements which you have been learning from and see if any of them have a semicolon in that position.)
Jim Stevens
Greenhorn

Joined: Jun 08, 2011
Posts: 28
Haha thank you. Got it running beautifully. I overlooked that semicolon.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Is there a way to search for a Characters location in a String without using lastindexOf
 
Similar Threads
char insertion in strings
find particular characters within a String ?
Mughal String.lastIndexOf
Find the last character in a srting.
Greedy quantifier