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 Extra Credit Solution(s)

Hi there

I struggled to do the Extra Credit part at first and then came up with two working solutions (see below).

  1. Which of the two methods is best/most efficient
  2. Are the ways I've chosen to do them the correct/best practice method?

//This part assigns the bad words to a string and is common to methods 1 & 2

  String badWords="geekjerkballs";

  //---Method 1 - While loop ---

 /* String word = console.readLine("Enter a word:  ").toLowerCase();
  console.printf(word+"\n");

    while(badWords.contains(word)) 
    {
      word = console.readLine("Enter a word:  ").toLowerCase();
    }*/


  //---Method 2 - Do-While loop ---

  // String word;
  boolean naughty;
  do {
      String word = console.readLine("Enter a word:  ").toLowerCase();
      naughty=badWords.contains(word);
     }
  // While you are getting a bad word that appears in the string, repeat the above
  while (naughty);

  // This next part is standard and breaks the loop when loop conditions not met

  console.printf("Next... \n");

1 Answer

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