Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Local Development Environments Advanced Tooling Finishing TreeStory

Daren Davis
Daren Davis
6,929 Points

[Solved] getting java.util.NoSuchElementException (Works in IntelliJ)

Went to stackOverFlow and found some articles but thought that was more for Reading file input. Here is where i'm having the error( scanner.nextLine()).

public String promptForWord(String phrase){
        String result;
        do {
            Scanner scanner = new Scanner(System.in);
            System.out.printf("Enter %s: ", phrase);
            result = scanner.nextLine();
            if(mCensoredWords.contains(result)){
                System.out.println("Invalid word!");
            }
        } while(mCensoredWords.contains(result));
        return result;
    }

any tips and pointers much appreciated! ;)

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

        public class Main {

    public static void main(String[] args) {
        Prompter prompter = new Prompter();
        String story = "Thanks __name__ for helping me out.  You are really a __adjective__ __noun__ and I owe you a __noun__.";
        Template tmpl = new Template(story);
        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;

    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> results = null;
        try {
            results = promptForWords(tmpl);
        } catch (IOException e) {
            System.out.println("There was a problem prompting for words");
            e.printStackTrace();
            System.exit(0);
        }
        System.out.printf("Your TreeStory:%n%n%s", tmpl.render(results));
    }

    /**
     * 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){
        String result;
        do {
            Scanner scanner = new Scanner(System.in);
            System.out.printf("Enter %s: ", phrase);
            result = scanner.nextLine();
            if(mCensoredWords.contains(result)){
                System.out.println("Invalid word!");
            }
        } while(mCensoredWords.contains(result));
        return result;
    }

}
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

1 Answer

Jeff Wilton
Jeff Wilton
16,633 Points

If you look at the preview for your code, you will see the line number that is the problem:

result = scanner.nextLine();

My guess would be that the censored words txt file contains a carriage return or similar character that is causing the scanner to throw an error. You have a couple of options: surround it with a try/catch, or more elegantly, add a check to make sure there is a line to read:

if(scanner.hasNextLine()){  //could also be a 'while' instead of an 'if'
   result = scanner.nextLine();
   if(mCensoredWords.contains(result)){
      System.out.println("Invalid word!");
    }
}

Having said that, you will run into some other problems like this one: "Bummer! Did you prompt the user for a new story (1)? I see the original prompt". The reason for that error is because you need to prompt the user to enter their own Story instead of hard coding it to this one:

String story = "Thanks name for helping me out. You are really a adjective noun and I owe you a noun.";

Lastly, if any other issues are encountered, I would encourage you to look at the Questions section for the previous video. Another student was nice enough to provide a working example of the exercise here: https://teamtreehouse.com/community/i-completed-this-challenge-any-feedback

Hope this helps!