harsh harsh - 3 days ago 6
Android Question

Profanity Test Not Working As Expected (In Chat Application)?

I am trying to create a profanity test for my app, but it seems to malfunction!! why?

code:

public boolean filter(String message)
{

String[] words={*CUSS WORDS*};

for(int i=0; i< (words.length-1); i++ )
{
if(message.indexOf(words[i].toLowerCase())!= -1)
{
return true;
}
}
return false;
}


OR Another code (BUT SAME FUNCTION):

public boolean filter(String message)
{

String[] words={CUSS WORDS};

for(int i=0; i< (words.length-1); i++ )
{
if(message.contains(words[i}))
{
return true;
}
}
return false;
}


So the PROBLEM IS:
I tried these 2 pieces of codes with similar results. For example for "Fuck", if I enter "fu" into my app it stops it from being entered or for "ass", if I enter "as" it stop it from being entered! (Filter works to stop any profanity from entering the chat)

Answer

Store your curse words in a set, then break up the users sentence into individual words. Check each word to see if it's in your set of curse words.

public boolean curse(String str){
    //Create your set here
   HashSet<String> wordSet = new HashSet<String>();

    //Use it's add function to add your curse words
    wordSet.add("ass");

    String array[] = str.split(" ");

    for(String s : array){
        if(wordSet.contains(s.toLowerCase()))
            return true;
    }

    return false;
 }
Comments