February 12th, 2013, 07:39 AM
Yes, I need to find the exact word only, although this doesn't really make sense in terms of real profane words. **** and ****tomb should both be censored.
Originally Posted by MrFujin
I guess this is the problem. My methods for doing this are really bad. All I can do is use indexOf to find where it starts...but from there I've tried using length and contentEquals, neither of which worked.
By spacing and punctuation.
February 12th, 2013, 07:46 AM
Find the searched for String: "cat" and then check if it is surrounded (delimited) by spaces or punctuation.
If you can only use the indexOf() method, it will not be possible to solve this without having hundreds of calls to indexOf() for all possible combinations.
February 12th, 2013, 07:54 AM
Hmm...well as I said, I am three weeks into an introductory course of Java programming. Trust me, I would like to search for cat with spacings and punctuation, but I really don't know how. This project is actually an extra challenge to something much easier, and all the rest of my projects I completed with ease. My projects aren't due for another week or so, so perhaps my teacher will go over this one in class.
Originally Posted by NormR
February 12th, 2013, 08:03 AM
It's a multi step process:
first find the location of "cat" in the sentence/String
then see if the character in front of "cat" is a space or punctuation
then see if the character after "cat" is a space or punctuation
If both those are tests are true, then the found String: "cat" is a word
February 12th, 2013, 08:09 AM
Can do this with indexOf, correct?
Originally Posted by NormR
This seems problematic, as indexOf will only take me to the where cat begins. Also, if cat begins a sentence, I will get run time errors.
I can probably figure this part out, but the middle part is an issue for me.
February 12th, 2013, 08:32 AM
Use the value returned by indexOf() to determine that.
Also that value with the length() method would tell you if cat ends a sentence.
To see: write a sentence with cat at the end, write numbers under each character and see what math expression would tell you if cat is at the end.
February 12th, 2013, 02:44 PM
February 12th, 2013, 03:20 PM
If cat is at the start of the sentence, there will not be a space:
"cat in a hat"
Leave off the spaces in the indexOf() call. Then use the substring method to get the character before and the one after.
February 12th, 2013, 03:29 PM
Can't I add " " before and after the string then test for (space)cat(space)? Seems like it will work.
February 12th, 2013, 03:32 PM
That would fix that case.
What about when in 's: " 'cat' in a hat"
or followed by .: the cat.
or followed by ,: a cat, rat or bat
February 12th, 2013, 03:36 PM
Its just the exact form of cat. Mercifully, I don't have to deal with punctuation or apostrophe's. This should fix the problem for me.
Perhaps once I show you my working program, you could show me (with code) the method you're talking about? If you don't mind of course. I think its a little above my current abilities.
February 12th, 2013, 03:41 PM
You'll have to come back to this problem after you study the other String class methods.
February 12th, 2013, 03:46 PM
Ahh. Many thanks for your help.
February 12th, 2013, 03:50 PM