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 Local Development Environments Advanced Tooling Finishing TreeStory

edumorlom
edumorlom
4,073 Points

I'm getting an error. It says I need to catch an exception but I did catch it.

I'm getting the following error. The program runs on my IDE... and I did catch the exception on line 77. Please, anyone?

JavaTester.java:77: error: unreported exception IOException; must be caught or declared to be thrown
    Main.main(null);
             ^
1 error
package com.teamtreehouse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader mReader = new BufferedReader(new InputStreamReader(System.in));
        String story = null;
        try{ System.out.println("Hello, enter your new story template");
        story = mReader.readLine();}
        catch(IOException ioe){
            ioe.printStackTrace();
        }
        Prompter prompter = new Prompter();
        Template tmpl = new Template(story);
        List<String> fakeResults = Arrays.asList("friend", "talented", "java programmer", "high five");
        prompter.run(tmpl, fakeResults);

    }
}
package com.teamtreehouse;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;


public class Prompter {
    private BufferedReader mReader;
    private Set<String> mCensoredWords;

    public Prompter() {
        mReader = new BufferedReader(new InputStreamReader(System.in));
        loadCensoredWords();
    }

    private void loadCensoredWords() {
        mCensoredWords = new HashSet<String>();
        Path file = Paths.get("resources", "censored_words.txt");
        List<String> words = null;
        try {
            words = Files.readAllLines(file);
        } catch (IOException e) {
            System.out.println("Couldn't load censored words");
            e.printStackTrace();
        }
        mCensoredWords.addAll(words);
    }

    public void run(Template tmpl, List<String> fakeResults) {
        List<String> results = null;
        try {
            results = promptForWords(tmpl);
        } catch (IOException e) {
            System.out.println("There was a problem prompting for words");
            e.printStackTrace();
            System.exit(0);
        }
        String result = tmpl.render(results);
        System.out.printf("Your TreeStory:%n%n%s", result);
    }

    /**
     * Prompts user for each of the blanks
     *
     * @param tmpl The compiled template
     * @return
     * @throws IOException
     */
    public List<String> promptForWords(Template tmpl) throws IOException {
        List<String> words = new ArrayList<String>();
        for (String phrase : tmpl.getPlaceHolders()) {
            String word = promptForWord(phrase);
            words.add(word);
        }
        return words;
    }


    /**
     * Prompts the user for the answer to the fill in the blank.  Value is guaranteed to be not in the censored words list.
     *
     * @param phrase The word that the user should be prompted.  eg: adjective, proper noun, name
     * @return What the user responded
     */
    public String promptForWord(String phrase) throws IOException{
        String word = null;
        try {
            do {
                System.out.printf("Enter a/n %s:", phrase);
                word = mReader.readLine();
            }            while (mCensoredWords.contains(word));
        }
        catch (IOException ioe){
            ioe.printStackTrace();
        }

        return word;
    }
}

2 Answers

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Hi edumorlom, I copied your Prompter class into IntelliJ and it shows line 77 being word = mReader.readLine();. Is that correct? If so, are you sure that mReader is returning a new line?

Also, it looks like you are catching the exception, but when you catch it, you're just printing out the stack trace. So, that part seems to be working correctly.

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,736 Points

It’s not line 77 of your code, it’s line 77 of Treehouse’s code, “JavaTester.java”, that is testing your program.

The problem is actually in Main.java on line 15. You declared that this method throws an exception. You didn't need to declare that, you already caught the exception, but because you declared it, any other method that calls your method is now responsible for either catching the exception or declaring it to be thrown.

When you ran the code in your IDE, everything worked okay because this method wasn’t inside other code, but in Treehouse’s test, there’s this test class JavaTester has some method that is testing your code, and then the Java compiler steps in to make sure the exceptions that your code says could get thrown are handled.

I think your code has some other little problems before it will pass the challenge. I tried fiddling with the code a little but I think I’m going to pack it in for time being.

One thing to look at though - on Main.java line 25 you still have the fake results here, and then you pass them into your prompter.run(). Inside that run method, you immediately set the contents to null, so it doesn’t seem to affect the output, but it’s still probably something that should be cleaned up.

edumorlom
edumorlom
4,073 Points

Thanks.

He doesn't specify what he wants though..... like, my code works and he's so vague in explaining. How am I supposed to know what he will be testing? I followed the instructions...