7 Replies - 2076 Views - Last Post: 20 March 2011 - 05:46 PM Rate Topic: -----

#1 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Profanity Filter

Posted 20 March 2011 - 04:54 PM

Assignment: Suppose that we are working for an online service that provides a bulletin board for its users. We would like to give our users the option of filtering out profanity. Suppose that we consider the words Cat, dog and Llama to be profane. Write a program that reads a string from the keyboard and tests whether the string contains one of the profane words. Your program should find words like cAt that differ only in case.

Optional: As an extra challenge, have your program reject only lines that contain a profane word exactly. For example, Dogmatic concatenation is a small category. This sentence should not be considered profane.

import java.util.Scanner;
public class CS1_Javed_X7_1
{
  public static void main(String[] args)
  {
  String a;
  int case_num = 0;
  
   Scanner keyboard = new Scanner(System.in);
   System.out.println("Enter a sentence:");
   a = keyboard.nextLine();
   a = a.toLowerCase();
   
   if (a.indexOf("cat") != -1) //this section detects for the profane words
     case_num = 1;
   else if (a.indexOf("dog") != -1)
     case_num = 1;
   else if (a.indexOf("llama") != -1)
     case_num = 1;
   
   switch(case_num)         //This secton gets the correct message to print.
   {
     case 1:
       System.out.println("your sentence consists of profanity");
       break;
     default:
       System.out.println("No profanity detected in the Sentence");
   }

  }
}



Question: How do i make My code so its not doing the optional part?.. What method should i use besides .indexOf to get the problem. I am asking this because my professor wants the code without the optional part. I could not figure out another way to do the program without using indexOf. I tried using .equalsIgnoreCase() but that would not help me because that compares the entire string and does not look for a sub-string.

This post has been edited by AhmerjavedC++: 20 March 2011 - 05:05 PM


Is This A Good Question/Topic? 1
  • +

Replies To: Profanity Filter

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9042
  • View blog
  • Posts: 33,540
  • Joined: 27-December 08

Re: Profanity Filter

Posted 20 March 2011 - 05:10 PM

You could use the String toLowerCase() or toUpperCase() methods to return a new String with the updates, then check for the lowercase or uppercase profanities respectively. Also, an array containing the profanities would allow you to use a loop to check the String. Much more efficient in terms of lines of code b/c you won't have to write out 1000 if statements if you have 1000 profanities.
Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3047
  • View blog
  • Posts: 4,562
  • Joined: 08-June 10

Re: Profanity Filter

Posted 20 March 2011 - 05:28 PM

One question. The optional part, is it asking for case-sensitive matches or full-world matches?
I was assuming the latter, but since you were talking about equalsIgnoreCase(), even though the code you posted seems to be case-insensitive, I was wondering.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9042
  • View blog
  • Posts: 33,540
  • Joined: 27-December 08

Re: Profanity Filter

Posted 20 March 2011 - 05:30 PM

Actually, the indexOf() method is case-sensitive.
Was This Post Helpful? 0
  • +
  • -

#5 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Profanity Filter

Posted 20 March 2011 - 05:34 PM

Well i actually change the case to all lowercase and then .indexOf() is able to find those words.. It does not matter if the user types in "DoG" or "Dog" it will be changed to "dog" and then it will be considered profane.

My question is tho. How to i make my code. so it detects dogmatic and considers it profane.. I have not done any arrays yet.
Was This Post Helpful? 1
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9042
  • View blog
  • Posts: 33,540
  • Joined: 27-December 08

Re: Profanity Filter

Posted 20 March 2011 - 05:39 PM

Add an if statement and check to see if a.contains("dogmatic").

Really, an array would be much better, though.
String[] profanities = new String[]{"dog","cat","dogmatic","llama"};

for(String s:profanities){
     if(myString.contains(s)){
        //we have a profanity
     }
}



You can check out the Java Tutorials Section, as well as the Oracle tutorial for more on arrays.
Was This Post Helpful? 2
  • +
  • -

#7 AhmerjavedC++  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 253
  • Joined: 02-October 09

Re: Profanity Filter

Posted 20 March 2011 - 05:45 PM

Thank you!.. .contains() works..
Was This Post Helpful? 1
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9042
  • View blog
  • Posts: 33,540
  • Joined: 27-December 08

Re: Profanity Filter

Posted 20 March 2011 - 05:46 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1