• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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....
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to test loops then perhaps the toCharArray() method to return a char array would be useful.
 
Jim Stevens
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So your saying converting s into an array then using a for loop and if statement to find the location?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haha thank you. Got it running beautifully. I overlooked that semicolon.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic