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 for java basics

I am curious to know how to do this extra credit assignment, can someone help me out? Thanks!

Extra Credit There is a method on string called contains. It checks to see if one string is contained within the other. This seems like a good way to build a master list of words that are not allowed. Why don't you see if what they typed is in a long string of bad words. After you get that working, attempt to take case into account. If your list of bad words that you maintain is all lower cased, you can check the lower case version of the input in the bad string. How do you make a string lowercase?

2 Answers

The contains() method in the String class will check a the whole String for a character sequence. This means we can make one long String of words we do not want to accept from the user. Also because this method checks on the character level there is no need to put blank spaces on the master list of banned words.

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#contains(java.lang.CharSequence)

// String of bad words we don't want to accept from the user
// here using freaking, heck, lannister, and sith
String badWords = "freakinghecklannistersith";

// Lets use the noun String from the example that receives a 
// line from the user (we are assuming here that the line only
//  contains only one word).
String noun = console.readLine("Enter a noun: ");

// We don't know how the casing will be from the user, we don't want to 
// allow LanNISter through since our master list String is all lower case 
// we can address case by forcing the noun variable to be lower case 
// then check the master list.
noun = noun.toLowerCase();

From here we can use the contains() method to check if the noun character sequence is in our master list of badWords.

while(badWords.contains(noun)) {
     noun = console.readLine("Hey thems fighting words try again: ");
}

Awesome help dude, you have no idea how good it feels to finally get the syntax right on this!

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