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 Delivering the MVP Exceptions

Not running did exactly what Craig did

Prompter.java: 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); boolean isHit = false; try { isHit = game.applyGuess(guess); } catch(IllegalArgumentException iae) { System.out.println(iae.getMessage()); }

return isHit;

}

public void displayProgress() { System.out.printf("Yoy have %d tries left to solve: %s%n", game.getRemaingTries(), game.getCurrentProgess());

}

}

Game.java: class Game { public static final int MAX_MISSES = 7; private String answer; private String hits; private String misses;

public Game (String answer) { this.answer = answer; hits = ""; misses = ""; }

public boolean applyGuess(char letter) { if (misses.indexOf(letter) != -1 || hits.indexOf(letter) != -1 ) { throw new IllegalArgumentException(letter + " has already been guessed"); } boolean isHit = answer.indexOf(letter) !=-1; if (isHit) { hits += letter; } else { misses += letter; } return isHit; }

public int getRemainingTries() { return MAX_MISSES - misses.lenght(); }

public String getCurrentProgress() { String progress = ""; for (char letter : answer.toCharArray()) { char display = '-'; if (hits.indexOf(letter) != -1) { display = letter; } progress += display ; } return progress; }

}

My mistakes; Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Picked up _JAVA_OPTIONS: -Xmx128m
./Game.java:15: error: unclosed string literal
throw new IllegalArgumentException(letter + " has already been guessed);
^
./Game.java:15: error: ';' expected
throw new IllegalArgumentException(letter + " has already been guessed);
^
./Game.java:26: error: illegal start of expression
public int getRemainingTries() {
^
./Game.java:26: error: ';' expected
public int getRemainingTries() {
^
./Game.java:30: error: illegal start of expression
public String getCurrentProgress() {
^
./Game.java:30: error: ';' expected
public String getCurrentProgress() {
^
./Game.java:42: error: reached end of file while parsing
}
^
Hangman.java:7: error: cannot find symbol
while (game.getRemainingTries() > 0) {
^
symbol: method getRemainingTries()
location: variable game of type Game
./Game.java:27: error: cannot find symbol
return MAX_MISSES - misses.lenght();
^
symbol: method lenght()
location: variable misses of type String
./Game.java:32: error: variable letter is already defined in method applyGuess(char)
for (char letter : answer.toCharArray()) {
^
./Game.java:39: error: incompatible types: String cannot be converted to boolean

1 Answer

Curtis Johnston
Curtis Johnston
1,078 Points

your first error is an unclosed string literal. you need to add the quote (") at the end. Also make sure you save your progress because from what I see, you did add the quote.