Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 9th, 2013, 01:15 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old February 9th, 2013, 05:19 PM
stdunbar's Avatar
stdunbar stdunbar is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: May 2004
Location: Superior, CO, USA
Posts: 2,398 stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level)stdunbar User rank is General 10th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 18 h 10 m 50 sec
Reputation Power: 1660
Send a message via Yahoo to stdunbar Send a message via Google Talk to stdunbar
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.

Reply With Quote
  #3  
Old February 9th, 2013, 06:32 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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...

Reply With Quote
  #4  
Old February 10th, 2013, 05:56 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 29 m 42 sec
Reputation Power: 345
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.

Reply With Quote
  #5  
Old February 10th, 2013, 09:07 AM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old February 10th, 2013, 09:24 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 29 m 42 sec
Reputation Power: 345
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.

Reply With Quote
  #7  
Old February 10th, 2013, 10:13 AM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #8  
Old February 10th, 2013, 10:31 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 29 m 42 sec
Reputation Power: 345
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()

Reply With Quote
  #9  
Old February 10th, 2013, 11:46 AM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #10  
Old February 10th, 2013, 12:29 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 29 m 42 sec
Reputation Power: 345
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/

Reply With Quote
  #11  
Old February 10th, 2013, 01:08 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #12  
Old February 10th, 2013, 01:21 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 29 m 42 sec
Reputation Power: 345
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.

Reply With Quote
  #13  
Old February 10th, 2013, 01:31 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #14  
Old February 10th, 2013, 01:37 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 29 m 42 sec
Reputation Power: 345
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

Reply With Quote
  #15  
Old February 10th, 2013, 03:04 PM
the_passenger the_passenger is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 44 the_passenger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
"cat" picks up category...catacomb...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Filtering Strings

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap