Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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);