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 trialCarolina Estrada
1,285 PointsWhen I ran the program, I didn't get the prompt but it did compile so I'm not sure what I'm doing wrong.
The output I get:
Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Picked up _JAVA_OPTIONS: -Xmx128m
Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Picked up _JAVA_OPTIONS: -Xmx128m
treehouse:~/workspace$
I don't get a prompt to enter a letter and I did exactly as he did.
I copied and pasted my input, I can't find what could have possibly gone wrong.
input;
1) Hangman.java;
public class Hangman { // main executable file where we will use instances of the Prompter and Game classes
public static void main(String[] args) { // Your incredible code goes here...
// make new instance of my game
Game game = new Game("treehouse");
Prompter prompter = new Prompter(game);
boolean isHit = prompter.promptForGuess();
if (isHit) {
System.out.println("We got a hit!");
} else {
System.out.println("Oops missed");
}
} }
2) Prompter.java;
import java.util.Scanner; class Prompter { // Will use this Prompter object for all input/output // who ever is working on Prompter will need to know about our game object // so we will store a private variable thats an instance of our game
private Game game;
//constructor which we use to create an instance of the Game
public Prompter(Game game) { this.game = game;
}
// create method that will prompt for a guess & returns whether guess was correct
public boolean promptForGuess() { Scanner scanner = new Scanner(System.in);
System.out.println("Enter a letter: ");
String guessInput = scanner.nextLine();
char guess = guessInput.charAt(0);
return game.applyGuess(guess);
} }
3) Game.java
class Game { // this class will need to know answer to the puzzle aka maintains the game logic
private String answer; private String hits; private String misses;
// then make a constructor to force creation of a game to provide an answer
// constructor has same name as class
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;
}
}
1 Answer
Anastasios Poursaitedes
10,491 PointsYou missed an access modifier on your Prompter class. It is "public class Prompter". When a class does not have an access modifier the default is package-private. Give it a try.