java - Censored Words Condition -
i need program print "censored" if userinput contains word "darn", else print userinput, ending newline.
i have:
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; } }
not sure condition if can be, don't think there "contains" method in string class.
the best solution use regex word boundary.
if(mystring.matches(".*?\\bdarn\\b.*?"))
this prevents matching sdarns
as rude word. :) demo here
Comments
Post a Comment