. Advertisement .
..3..
. Advertisement .
..4..
Print “censored” if userinput contains the word “darn”, else print userinput. end with newline? Here is the code that I run:
import java.util.Scanner;
public class CensoredWords {
public static void main (String [] args) {
String userInput = "";
Scanner scan = new Scanner(System.in);
userInput = scan.nextLine;
if(){
System.out.print("Censored");
}
else{
System.out.print(userInput);
}
return;
}
}
Can someone share with me your knowledge and experience?
A regex that includes a word boundary is the best option.
if(myString.matches(".*?\\bdarn\\b.*?"))
This will prevent you from using
sdarns
as rude words. Demo hereThe contains method of the String class determines whether or not a substring is a part of the entire string, so let’s do as the following to solve your problem: