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 sdarnsas rude word. :) demo here


Comments

Popular posts from this blog

Prolog - Listing -

orchardcms - change base url in local copy in Orchard CMS -

linear regression - Trying to get confidence/prediction intervals with `predict.lm` in R, but I keep getting an error regarding my dichotomous variable -