Here's what the teacher assigned us:
Suppose we are an online service that provides a bulletin board for its users. We would like to give our users the option of filtering out profanity. We will 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 our profane words. Your program should find words like cAt that differ only in case. You must also not identify words that simply contain what might otherwise be considered a profane word. For example, Dogmatic concatenation is a small category should not be considered profane. Allow the user to use the following punctuation: ( , . ? " ' ( ) ! : ;) This will mean that you would be expected to find “The “Cat” is not a doggone llamaman.” or “Cat, and dog can not be llama.” (Note: You will only be responsible for the first occurrence of a given profane word in a sentence. However, more than one profane word may be contained in a sentence. So “Concatenate the cats”, would not find a profane word but “The doggone cat, and dog are not a llama.” would return 2 profane words cat and llama)
so, I tried this code:
import java.util.Scanner;
public class Degrees
{
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[]args)
{
System.out.println("Enter a sentence");
String sentence = keyboard.nextLine();
sentence = sentence.toLowerCase();
if(sentence.indexOf("cat ") != -1)
System.out.println("the profane word cat was detected");
else
System.out.println("the profane word cat wasn't detected");
if(sentence.indexOf("dog ") != -1)
System.out.println("the profane word dog was detected");
else
System.out.println("the profane word dog wasn't detected");
if(sentence.indexOf("llama ") != -1)
System.out.println("the profane word llama was detected");
else
System.out.println("the profane word llama wasn't detected");
}
}
however the code isn't working how it should be. if i wrote "dogmatic dog" it should only check the first occurrence of dog and see that it is within a word and then ignore the second dog. My code is just ughh. I don't know what I'm missing and what I should add. I've been going at this for 6 hours straight I swear. Please help. I just can't think of anything else. So please, I am open to suggestions and hints.
I also tried using a switch statement but for some reason it was only executing the default.