The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Filtering Strings
Discuss Filtering Strings in the Java Help forum on Dev Shed. Filtering Strings Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 9th, 2013, 01:15 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 44
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
|
|
|
Filtering Strings
Hello,
I am trying to write a program that will test for profane words, in this instance, dog, cat, and llama. I was able to do this with the following code:
Code:
package badwords;
import java.util.Scanner;
public class BadWords {
public static void main(String[] args)
{
String s1;
System.out.println("Enter a sentence:");
Scanner keyboard = new Scanner(System.in);
s1 = keyboard.nextLine();
if ( s1.toLowerCase().indexOf ( "cat" ) > -1 ) {
System.out.println("Contains profanity");
}
else
if ( s1.toLowerCase().indexOf ( "dog" ) > -1) {
System.out.println("Contains profanity.");
}
else
if ( s1.toLowerCase().indexOf ( "llama" ) > -1) {
System.out.println("Contains profanity");
}
else
{
System.out.println(s1);
As a follow up to this, I have to make it so that only the exact words are filtered. For example, "category" will come back as profane with my code. I am really stuck on how to do this. I would really appreciate any tips this forum could give me.
I think it will need a Boolean test of some kind, but I don't know how I can tell the difference between cat and category, or dog or dogmatic.
|

February 9th, 2013, 05:19 PM
|
 |
Contributing User
|
|
Join Date: May 2004
Location: Superior, CO, USA
|
|
|
How are words separated in most languages? Why couldn't you include that separator character as part of the string to test against - i.e. " cat " (note the difference in your string vs. this one).
Note that you'll need to also test without the leading separator (in case the word is the first one in the sentence) and without the trailing one (in case it is the last one).
There are more efficient ways of implementing this but start here and move forward.
__________________
Need Java help? Want to help people who do? Sit down with a cup of Java at the hotjoe forums.
|

February 9th, 2013, 06:32 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 44
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
|
|
|
Thank you for the response.
It does enable category to not come up as being profane, but sentences like "i am a cat" or "cat" do not come up as profanity.
Do I need to introduce an additional || to my if statements? Or maybe an if statement within my if statement...
|

February 10th, 2013, 05:56 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
|
You need to define all the ways a word can be put in a String. For example:
Starts the String and is followed with a delimiter like space, comma(,), period(.), end of String, etc
Follows a delimiter like a space, comma, etc and is followed by a delimiter or by the end of the String.
|

February 10th, 2013, 09:07 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 44
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
|
|
|
Ahh, so I guess I will need a lot of if statements?
Thank you.
Ok so that is working very well. My only list problem is that if I just type in "cat." I don't know how I can filter cat and not category. The spaces and commas are working fine though.
|

February 10th, 2013, 09:24 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
|
Not necessarily. Use the indexOf statement to find the location of the String.
Its location will tell you if the String is at the beginning or end of the String.
If the searched for String is in the middle, use a String of valid delimiters, get the character in front of the String and see if it's in the String of delimiters by using the indexOf() method.
|

February 10th, 2013, 10:13 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 44
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
|
|
|
Are you suggesting:
int index1 = s1.indexOf("cat");
I don't see how knowing the position would help me filter.
|

February 10th, 2013, 10:31 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
|
Two delimiters are the beginning of the searched String and the end.
The indexOf value would tell you if the found String was at the beginning: index = 0
or at the end: index = searchedString.length() - foundStr.length()
|

February 10th, 2013, 11:46 AM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 44
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
|
|
|
Hmmm...I'm new to programming and haven't been introduced to that method yet. These homework assignments blow.
|

February 10th, 2013, 12:29 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
Quote: | haven't been introduced to that method |
What method are you talking about?
You are using it in the posted code:
Code:
if ( s1.toLowerCase().indexOf ( "cat" ) > -1 ) {
Read the API doc for all the methods in the String class:
http://docs.oracle.com/javase/7/docs/api/
|

February 10th, 2013, 01:08 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 44
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
|
|
Are you saying I need to use a delimiter in addition to the indexOf string I am searching for? As I said, " cat " "cat " " cat" work just fine, but none of them will catch just the word cat by itself, except indexOf "cat." When I use "cat" it also catches category which I don't want it to do.
I don't have to deal with punctuation in this assignment, but I am really stuck. I can either get it to not filter category but cat gets through, or both cat and category get filtered.
Thank you for your help.
|

February 10th, 2013, 01:21 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
|
With " category "
the character before cat is a space , a valid delimiter. That's good
But the character after cat: "e" is not a valid delimiter so that's not a match
With "cat" there are no characters before or after cat so that's a match.
|

February 10th, 2013, 01:31 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 44
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
|
|
|
Yes, I am well aware of that. Both "cat" as well as " cat" will catch category. I have to be able to only catch the exact word, cat, not category or catacomb.
|

February 10th, 2013, 01:37 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: Eastern Florida
|
|
|
To detect a match for a String you need to test if the matched section that was found is a word (has delimiters on both sides) or is a subpart of a bigger word.
the "e" following "cat" in "category" is not a delimiter so that is not a match
In this string there are spaces on both sides of cat which makes it a match: " cat in a hat"
A space is a delimiter
|

February 10th, 2013, 03:04 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 44
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
|
|
|
"cat" picks up category...catacomb...
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|