Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Basics Perfecting the Prototype Censoring Words - Looping Until the Value Passes

Nayoon kim
Nayoon kim
3,098 Points

String.contains () ; question

I tried to censor words in bulk

it worked when I tried like

=============================================

boolean censorship;
String adjective;

do  {
adjective = console.readLine("Enter an adjective");
censorship = (adjective.contains ("dorky")|| adjective.contains("bitchy") ||adjective.contains("dummy"));
if (censorship) {
console.printf("not a nice word. try again\n\n");
}

 } while (censorship);

============================================

but like I want to know the better way, so tried something like this

insert java here

import java.io.Console;

public class TreeStory {

    public static void main(String[] args) {
        Console console = System.console();

      String age = console.readLine("Enter your age: ");
      int age_int = Integer.parseInt (age);
      if (age_int<13) {
        console.printf("you must be over 13 to use this program\br");
        System.exit(0);}

String name= console.readLine ("Enter a name: " );
String adjective;
String notallowed;
boolean sensorship;
 do {
   adjective= console.readLine("Enter an adjective: " );

sensorship = adjective.contains (notallowed);
  notallowed = "dorky"|| "jerk" || "dumb" || "bitchy";



   if (sensorship = true) {
          console.printf ("that is not very nice word to use , try nicer adjective. try agin \n\n" );}

 } while (sensorship = true);



String noun= console.readLine ("Enter a noun: " );
String adverb= console.readLine ("Enter an adverb: " );
String verb= console.readLine ("Enter a verb: " );


       }

}

but it doesn't allow me to assign multiple strings into one string variable.. I guess I have to learn ARRAY to do that...

1 Answer

Daiane Silva
Daiane Silva
5,668 Points

Hi Nayoon kim! You can do something like this:

List<String> notAllowed = Arrays.asList( "dorky", "jerk" , "dumb"  , "bitchy");
sensorchip = notAllowed.constains(adjective);