Page 1 of 4 123 ... Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2

    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.
  2. #2
  3. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    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.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    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...
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    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.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    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.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    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.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    Are you suggesting:

    int index1 = s1.indexOf("cat");

    I don't see how knowing the position would help me filter.
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    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()
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    Hmmm...I'm new to programming and haven't been introduced to that method yet. These homework assignments blow.
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    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/
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    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.
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    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.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    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.
  26. #14
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    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
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    "cat" picks up category...catacomb...
Page 1 of 4 123 ... Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo