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 wether the string contains one of our profane words. Your program should find words like cAt that differ only in case.
Attempt:
import java.util.Scanner;
public class Assign5 {
public static void main(String[] args) {
String cat,dog,llama,x;
System.out.println("Enter a word");
Scanner keyboard = new Scanner (System.in);
x = keyboard.next();
x.equalsIgnoreCase(x);
if(x.indexOf("cat")!=-1&&x.indexOf("dog")!=-1&&x.indexOf("llama")!=-1);
{
System.out.println("Profanity Detected");
}
else
{
System.out.println("No Profanity Detected");
}
}
It’s underlining else for some reason so I can’t run it. What am I doing wrong?
I am not allowed to use: for, while, and arrays. We haven’t gotten to those yet. I am only allowed to use: boolean, switch, and if-else. Also, I am using x.equalsIgnoreCase(x) to discard case identification. Will it work?
The problem is this:
Do you notice something at the end of the “if”?
Hint the “{block}” isn’t part of the if! This is what is causing the “else” to underline.
Perhaps there are additional errors as well, but that’s a syntax error flat-out.
I format my code like this so I can spot these errors easily. Adopting good code formatting is a very productive task 🙂