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 Using Method Overloading

Kian Bianco
Kian Bianco
2,317 Points

Won't run after following along in video.

Here are my errors:

./Game.java:32: error: not a statement
letter - normalizeGuess(letter);
^
./Prompter.java:26: error: while expected
}
^
./Prompter.java:27: error: ')' expected
return isHit;
^
./Prompter.java:24: error: no suitable method found for println(String,String)
System.out.println("%s. Please try again. %n", iae.getMessage());
^
method PrintStream.println() is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(boolean) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(char) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(int) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(long) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(float) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(double) is not applicable
(actual and formal argument lists differ in length)
Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Picked up _JAVA_OPTIONS: -Xmx128m
./Game.java:32: error: not a statement
letter - normalizeGuess(letter);
^
./Prompter.java:26: error: while expected
}
^
./Prompter.java:27: error: ')' expected
return isHit;
^
./Prompter.java:24: error: no suitable method found for println(String,String)
System.out.println("%s. Please try again. %n", iae.getMessage());
^
method PrintStream.println() is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(boolean) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(char) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(int) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(long) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(float) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(double) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(char[]) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(String) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(Object) is not applicable
(actual and formal argument lists differ in length)
4 errors

Can you post your code?

Kian Bianco
Kian Bianco
2,317 Points

Prompter:

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); boolean isHit = false; boolean isAcceptable = false;

do {
  System.out.print("Enter a letter:  ");
  String guessInput = scanner.nextLine();

  try {
    isHit = game.applyGuess(guessInput);
    isAcceptable = true;
  }
  catch (IllegalArgumentException iae) {
    System.out.println("%s. Please try again. %n", iae.getMessage());
  }
}
return isHit;

}

public void displayProgress() { System.out.printf("You have %d tries left to solve: %s%n", game.getRemainingTries(), game.getCurrentProgress()); } }

Hangman:

public class Hangman {

public static void main(String[] args) { // Your incredible code goes here... Game game = new Game("treehouse"); Prompter prompter = new Prompter(game); while (game.getRemainingTries() > 0) { prompter.displayProgress(); prompter.promptForGuess(); } } }

Game: class Game { public static final int MAX_MISSES = 7; private String answer; private String hit; private String miss;

public Game(String answer) { this.answer = answer.toLowerCase(); hit = ""; miss = ""; }

private char normalizeGuess(char letter) { if (! Character.isLetter(letter)) { throw new IllegalArgumentException("A letter is required"); } letter = Character.toLowerCase(letter); if (miss.indexOf(letter) != -1 || (hit.indexOf(letter) != -1)) { throw new IllegalArgumentException(letter + " has already been guessed"); } return letter; }

public boolean applyGuess(String letters) { if (letters.length() == 0) { throw new IllegalArgumentException("No letter found"); } return applyGuess(letters.charAt(0)); }

public boolean applyGuess(char letter) { letter - normalizeGuess(letter); boolean isHit = answer.indexOf(letter) != -1; if (isHit) { hit += letter; } else { miss += letter; } return isHit; }

public int getRemainingTries() { return MAX_MISSES - miss.length(); }

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

In reviewing the video and code from the video's workspace:

1) ./Game.java:32: error: not a statement letter - normalizeGuess(letter); ^

This should be an assignment

letter = normalizeGuess(letter);

2) ./Prompter.java:26: error: while expected

The while condition is missing

do {
  System.out.print("Enter a letter:  ");
  String guessInput = scanner.nextLine();

  try {
    isHit = game.applyGuess(guessInput);
    isAcceptable = true;
  }
  catch (IllegalArgumentException iae) {
    System.out.println("%s. Please try again. %n", iae.getMessage());
  }
} while(! isAcceptable);

3) ./Prompter.java:24: error: no suitable method found for println(String,String) System.out.println("%s. Please try again. %n", iae.getMessage());

This should use printf instead of println

System.out.printf("%s. Please try again. %n", iae.getMessage());