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

a string of words, help

Please help me. The task says to create a string of words that cannot be entered by the user as a noun. Currently, the program prevents either nerd or pig from being accepted but it does not stop something like nerdy or piggy. How can I do this? Since the course has not yet gone into arrays, an answer which does not use arrays will be greatly appreciated.

Thank you

import java.io.Console;

public class TreeStory {

public static void main(String[] args) {
    Console console = System.console();
    /* Some terms:
       noun - Person, place or thing
       verb - An action
       adjective - A description used to modify or describe a noun
       Enter your amazing code here:
    */
    String ageAsString = console.readLine("How old are you?  ");
    int age = Integer.parseInt(ageAsString);
    if (age < 13) {
      //Insert exit code
      console.printf("Sorry you must be at least 13 to use this program.\n");
      System.exit(0);
    }  

    String name = console.readLine("Tell me your name:  ");
    String adjective = console.readLine("Enter an adjective:  ");
    String noun;
    String badWords = "nerd pig";
    boolean isInvalidWord;

    do {
    noun = console.readLine("Enter an noun:  ");
     isInvalidWord = badWords.contains(noun.toLowerCase());
      if (isInvalidWord){
        console.printf("That language is not allowed...try again\n");
          }
      }
     while (isInvalidWord);

  String adverb = console.readLine("Enter an adverb:  ");
  String verb = console.readLine("Enter an verb:  ");
  console.printf("%s is %s %s %s %s\n", name, adjective, noun, adverb, verb);
}

}