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 Java Objects Creating the MVP Current Progress

Zachary Martin
Zachary Martin
3,545 Points

Program runs but dashes won't display?

I wrote the colde along side craig and I unerstand it but for some reason it won't display dashes bu it will run the program which would also indicate there obviously errors in my code so you would think it should run..Can anyone help

Zachary Martin
Zachary Martin
3,545 Points

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("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: %s%n", game.getCurrentProgress()); }

}

Zachary Martin
Zachary Martin
3,545 Points

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 got it right!");

}
else{ 
System.out.println("Sorry try again");
}
prompter.displayProgress();

} }

Zachary Martin
Zachary Martin
3,545 Points

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(hit.indexOf(letter)!=-1) { display = letter; } progress += display;

}
return progress;

}

}

1 Answer

Gabbie Metheny
Gabbie Metheny
33,778 Points

I know it's a little late, so hopefully you've already solved it, but it looks like in Game.java, you typed hit instead of hits in your getCurrentProgress method:

if(hit.indexOf(letter)!=-1)
//   ^ should be hits

In the future, try formatting your code with the Markdown Cheatsheet. It makes it much easier for others to read your code, which means they're more likely to spot what's wrong! In situations like this, where there's multiple files and a console involved, it can be helpful to create a snapshot of your workspace, too.

Hope that helps!