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

Extra Credit question at end of Java Basics, stumped.

Ive read the question where Craig and Ken both answered this but i still can't get Java to run more than one word from my String badWords = "nerd dork jerk";

I would have to type "nerd dork jerk" for it to work, and im trying to get nerd or dork to work by themselves.

Here's my code, i feel stupid!

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!
    */
  //__Name__is a__adjective__ __noun__.  They are always __adverb__ __verb__.
  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("Enter a name:  ");
  String noun;

  String badWords = "nerd";

  boolean isInvalidWord;
  do {
    noun = console.readLine("Enter a noun:  ");
    isInvalidWord = noun.contains(badWords); 

    if (isInvalidWord)  {
      console.printf("That language is not allowed.  Try again.  \n\n");
    }
  } while(isInvalidWord);

  String adjective = console.readLine("Enter an adjective:  "); 
  String adverb = console.readLine("Enter an adverb:  ");
  String verb = console.readLine("Enter a verb ending with -ing:  ");

  console.printf("Your TreeStory:\n---------------\n");
  console.printf("%s is a %s %s.  ", name, adjective, noun);
  console.printf("They are always %s %s. \n", adverb, verb);
}

}

Um perhaps try badWords.contains(noun)

2 Answers

Personally I would do

String badWords = ":butt:dumb:jerk"

String noun = ":" + input;

The input would be whatever they inputed and stored in a variable input. Now if you run

badWords.contains(noun)

It should work id assume..

Thanx Andrew, it worked!

No Problem! If you would like I can always look into a more efficient approach/different ways to get similar results

If you need any help with any programing/tech feel free to add me on skype andrew_carson_gay

I am usually quite to respond!

Just please mention you are from Treehouse or I might not respond :P

Though I do apologize for just handing out the answer XD, though I assume you understand what was the problem correct?

Here's my solution:

import java.io.Console;

public class TreeStoryImproved {

    public static void main(String[] args) {
        Console console = System.console();
        String ageAsString = console.readLine("Enter your age:  ");
        int age = Integer.parseInt(ageAsString);
        if (age < 13) {
            console.printf("Sorry. You're underage.\n");
            System.exit(0);
        }
        String name = console.readLine("Enter a name:  ");
        String adjective = console.readLine("Enter an adjective:  ");
        String noun;
        boolean isInvalidWord;
        String areBadWords = "arse ass asshole bastard bitch bloody bollocks cunt damn fuck goddamn                     godsdamn hell holy shit motherfucker shit shitass son of a bitch son of a motherless goat son of a whore jerk dork";
        do {
            noun = console.readLine("Enter a noun:  ");
            isInvalidWord = areBadWords.toLowerCase().contains(noun.toLowerCase());
            if (isInvalidWord) {
                console.printf("That language is not allowed. Try again. \n\n");
            }
        } while(isInvalidWord);
        String adverb = console.readLine("Enter an adverb:  ");
        String verb = console.readLine("Enter a verb:  ");
        console.printf("%s is a %s %s. They are always %s %s", name, adjective, noun, adverb, verb);
        }
}