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 trialLaura Mckay
599 PointsNot printing Hit or miss lines of code!
When I add the display method it cancels out the print lines that say if the guess is correct or not. Checked against Craig's code and it seems the same to me! Seem to have this problem with lot of the courses on here! :(
Also the workspace is no longer telling me what errors I have, only telling me how many, so having to run my code through IntelliJ to find the errors!
public class Hangman {
public static void main(String[] args) { // Your incredible code goes here... Game game = new Game("treehouse"); Prompter prompter = new Prompter(game); prompter.displayProgress(); boolean isHit = prompter.promptForGuess(); if (isHit) { System.out.println("You have guessed correctly!"); } else { System.out.println("Incorrect guess! :("); } prompter.displayProgress(); } }
class Game { private String answer; private String hits; private String misses;
public Game(String answer) { this.answer = answer; hits = ""; misses = ""; } public boolean applyGuess(char letter) { boolean isHit = answer.indexOf(letter) != -1; if (isHit) { hits += letter; } else { misses += letter; } return isHit; } public String getCurrentProgress() { String progress = ""; for (char letter : answer.toCharArray()) { char display = '_'; if (hits.indexOf(letter) != -1) { display = letter; } progress += display; } return progress; } }
import java.util.Scanner;
class Prompter { private Game game;
public Prompter(Game game) { this.game = game; }
public boolean promptForGuess() { Scanner scanner = new Scanner(System.in); System.out.print("Please enter a letter: "); String guessInput = scanner.nextLine(); char guess = guessInput.charAt(0); return game.applyGuess(guess); } public void displayProgress() { System.out.printf("Try to solve the word: %s%n", game.getCurrentProgress()); } }
Laura Mckay
599 PointsIt wasn't working in the workspace, but when I ran it in IntelliJ it worked. I think the workspace is not working properly as wasn't showing me what my errors were, just telling em how many I had. Sorry for the inconvenience & thanks for trying to help. :)
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsI also wanted to add. that the major difference between Intellijdea works and Workspaces work is that you don't hit save.
In workspaces after changes you always have to hit Ctrl+S to save file.
Otherwise you will compile the wrong code.
Inteliijdea however saves files automatically ...
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsAlexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsI'm sorry I don't understand the question.
I tried to run your code and it works exactly like Craigs, i.e
And for incorrect guess
Nothing is canceled out, everything is exactly like in video ...