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

Error shown in repl but not in intellij

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

The above error. But I have thrown IOException where ever I could, didn't I?

com/teamtreehouse/Main.java
package com.teamtreehouse;

import java.io.IOException;


public class Main {

    public static void main(String[] args) throws IOException {

        String story1;

       Prompter prompter = new Prompter();
       story1 = prompter.getStory();

        Template tmpl = new Template(story1);

        prompter.run(tmpl);

    }
}
com/teamtreehouse/Prompter.java
package com.teamtreehouse;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;


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

    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 String getStory() throws IOException {
        System.out.println("Enter Story Template: ");
        String story = mReader.readLine();
        return story;
    }
    public void run(Template tmpl) {
        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);
        }


        finalResults = tmpl.render(results);
        System.out.printf("Your TreeStory:%n%n%s", finalResults);
    }

    List<String> words;

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



    String name = "";


    public String promptForWord(String phrase) throws IOException {

        System.out.println("Enter a name: ");
        name = mReader.readLine();
        while (mCensoredWords.contains(name)) {
            System.out.println("Enter a name: ");
            name = mReader.readLine();
        }
        return name;

        }
    }
pseudo-tests.md
#  This is essentially what I am testing 
1.  The user is prompted for a new string template (the one with the double underscores in it).

  a. The prompter class has a new method that prompts for the story template, and that method is called.

2.  The user is then prompted for each word that has been double underscored.

   a. The answer is checked to see if it is contained in the censored words.
      User is continually prompted until they enter a valid word

3.  The user is presented with the completed story

2 Answers

Mike D'Aguillo
Mike D'Aguillo
5,475 Points

Is there another class that you are coding? It looks like the error occurs at line 77 in a class called JavaTester. You've posted the code for your Main.java class and the Prompter.java class.

Check out the beginning of the error message "JavaTester.java:77: error: ". It tells you where exactly the error occurred during run time.

Hi Mike,

Thanks for the reply. I don't see the JavaTester class in the exercise. I think it is on the server side and it is to test my program. That;'s the frustrating part.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hi Yong Li Soh !

The main method should not throw an IO exception:

public static void main(String[] args) throws IOException {

You broke my Tester, can you catch it and handle it in your getStory method ;)

Hope that helps! You are super close!