We are working for an online service that provides a bulletin board for its user
ID: 3584981 • Letter: W
Question
We are working for an online service that provides a bulletin board for its users. We would like to give our users the option offteting out proaity. The words cat, dog. andliama, and any word which contains these words, have all been determined to be profane. Write a program that gets a string from the keyboard and tests whether the string contains one of our profane words. Make sure that your program finds wordslke cAt, that cifferol in the case of the letters Begin by asking the user to type in a sentence Once you have the sentence, you should proceed to test it to see if it contains one of the profane words. If one of the words is found in the sentence, print the message "Sentence is rejected." to the screen. If none of the words is in the sentence, print the message "Sentence is oK." to the screen. The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user's inputted values are shown below in italics: Enter a sentence: My dog has fleas. Sentence is rejected. Here is another example program run: Enter a sentence: My Dog has fleas. Sentence is rejected. Here is another example program run: Enter a sentence: We will concatinate today.Explanation / Answer
SentenceCheck.java
import java.util.Scanner;
public class SentenceCheck {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a sentence: ");
String sentence = scan.nextLine();
String words[] = {"cat","dog","llama"};
boolean found = false;
for(int i=0;i<words.length;i++) {
if(sentence.toLowerCase().indexOf(words[i].toLowerCase()) != -1) {
found = true;
break;
}
}
if(found) {
System.out.println("Sentence is rejected");
} else {
System.out.println("Sentence is OK");
}
}
}
Output:
Enter a sentence:
My dog has fleas
Sentence is rejected
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.