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

Help with TODO on last Java Challenge in the series?

Having trouble with this TODO on the java challenge. Was wondering if anyone could help me?

   // TODO:csd - Prompt the user for the response to the phrase, make sure the word is censored, loop until you get a good response.

This is what I have so far? Am I far away ?

    // TODO:csd - Prompt the user for the response to the phrase, make sure the word is censored, loop until you get a good response.
public String promptForWord(String phrase) throws IOException {
        System.out.printf("Enter your %s",phrase);
        String response = null;
        response = mReader.readLine();
        while (mCensoredWords.contains(response)) {
            System.out.printf("That is bad language.  Try again: ");
            response = mReader.readLine();
        }
        System.out.printf("Thanks for the response.");

        // TODO:csd - Prompt the user for the response to the phrase, make sure the word is censored, loop until you get a good     response.
        //Post this to forum
        return phrase;
    }

4 Answers

Conor,

Very close. You would not return phrase though. Therefore, you would return.....???

Hope this helps.

Regards,

Chris

Hi Christopher,

My only problem is I am unsure what my Main method should look like. This is what I have so far? Have been stuck on this the past few days :/

package com.teamtreehouse;

import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        Prompter prompter = new Prompter();
        String story = prompter.promptForStory();
        Template tmpl = new Template(story);
        try {
            prompter.promptForWord(story);
        } catch (IOException e) {
            e.printStackTrace();
        }
        prompter.run(tmpl);
    }
}

Regards Conor

Conor,

Sorry to hear that. Lets see if we can resolve it.

       package com.teamtreehouse;

       import java.io.IOException;

       public class Main {

           public static void main(String[] args) {

               Prompter prompter = new Prompter();
               // Just initialize to "" here. 
               // You want to call promptForStory in the try/catch
               String story = prompter.promptForStory();
               // Remove this. We don't want to do this until we have completed try/catch
               Template tmpl = new Template(story);

                try {
                // promptForWord is used within the Prompter object. We want to call 
                // promptForStory here and assign it to story.
                    prompter.promptForWord(story);

                } catch (IOException e) {
                     e.printStackTrace();
                }

               // Now we want to create the template with the story and pass it to 
               // prompter run
                  prompter.run(tmpl);
           }
         }

I hope this helps.

Regards,

Chris

Christopher,

Why do you initialise to " " ? What is the reason for this? I am not quite sure what you mean by this? Do I not need:

Prompter prompter = new Prompter();

Conor,

Good questions!

Unlike instance variables that have a default value, Java forces developers to initialize local variables. Therefore, String story has to be initialized before we use it. You do not have to initialize it to "" though. You can also initialize it to null. I was just trying to make it clear that you do not want to assign prompter.promptForStory() to story at this point. However, considering your last question it is evident that you thought I was referring to prompter.

All of my comments are for code directly below them. For example: I did not place any comments above the following:

       Prompter prompter = new Prompter();

because it is correct. Yes, you need to keep it just as it is.

My apologies for the confusion. Let me try this using TODO's. Each TODO I provide must be completed in the order they are given. Therefore, complete TODO 1, then 2, then 3.....

       package com.teamtreehouse;

       import java.io.IOException;

       public class Main {

         public static void main(String[] args) {

           Prompter prompter = new Prompter();

           //TODO 1: Complete the following declaration of String story by assigning it to null
           String story =


           //TODO 2: Complete the assignment of prompter.promptForStory() to story within try/catch
           try {
             story = 
            } catch (IOException e) {
                e.printStackTrace();
            }


           //TODO 3: Complete the following declaration of Template tmpl by assigning it a new Template. 
           Template tmpl =              // don't forget to pass story as an argument


           //TODO 4: call prompter.run() and pass tmpl as an argument.                

    }
}

Please let me know if you have any further questions.

Regards,

Chris

Hi Christopher, soon after I wrote that I figured it out. It was simply because I thought the comments were below. Many thanks for all your help!

Conor

Good to hear you got it. Always glad to help.

Regards,

Chris