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 (Retired) Delivering the MVP Validation

Daniel Schwemmlein
Daniel Schwemmlein
4,960 Points

all sorts of errors in my workspace

While i was watching the video i filled in everything to my workspace. These are the errors I got: ./Game.java:21: error: cannot find symbol
throw new IllegalArgumentException(letter + "has already been guessed");

./Game.java:22: error: cannot find symbol

./Game.java:40: error: cannot find symbol
./Prompter.java:11: error: cannot find symbol

I guess some of it should be errors involving my curly braces or missing dots. If you want I can also show my code. I have really only typed what i saw in the video

Daniel Schwemmlein
Daniel Schwemmlein
4,960 Points

Thats my code for game.java: public class Game { public static final int MAX_MISSES = 7; private String mAnswer; private String mHits; private String mMisses;

public Game(String answer) { mAnswer = answer; mHits = ""; mMisses = "";
}

private char validateGuess(char letter) { if (! Character.isLetter(letter)) { throw new IllegalArgumentException("A letter is required"); } } letter = Character.toLowerCase(letter); if (mMisses.indexOf(letter) >= 0 || (mHits.indexOf(letter) >= 0) { throw new IllegalArgumentException(letter + "has already been guessed"); return letter; } public boolean applyGuess(char letter) { letter = validateGuess(letter); boolean isHit = mAnswer.indexOf(letter) >= 0; if (isHit) { mHits += letter; } else { mMisses += letter; } }
public String getCurrentProgress() { String progress = ""; for (char letter: mAnswer.toCharArray()) { char display = '-'; if (mHits.indexOf(letter) >= 0) display = letter; } progress += display;

return progress; }

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

Daniel Schwemmlein
Daniel Schwemmlein
4,960 Points

and thats prompter.java: import java.io.Console;

public class Prompter { private Game mGame;

public Prompter(Game game) { mGame = game; }

public void play() { while (mGame.getRemainingTries > 0) { displayProgress(); promptForGuess(); } }

public boolean promptForGuess() { Console console = System.console(); boolean isHit = false; boolean isValidGuess = false; while (! isValidGuess) { String guessAsString = console.readLine("Enter a letter: "); char guess = guessAsString.charAt(0); try { isHit = mGame.applyGuess(guess); isValidGuess = true; } catch (IllegalArgumentException iae) { console.printf("%s. Please try again. \n", iae.getMessage()); } } return isHit; } public void displayProgress() { System.out.printf("You have %d tries left to solve: %s\n", mGame.getRemainingTries(), mGame.getCurrentProgress()); }

}

2 Answers

Hi Daniel,

Could you write three backsticks and "java" (the backsticks are probably located next to your number 1 on the keyboard) above the first line of code, and three backsticks on the line below the last of the code. To access the post editor click on the ... after your post, and click on edit. Or just start a new post and copy the code in between the backsticks.

I hope this isn't to much infomation, but this will help define what is code and what is explanatory writing.

Daniel Schwemmlein
Daniel Schwemmlein
4,960 Points
  public class Game 
  public static final int MAX_MISSES = 7;
  private String mAnswer;
  private String mHits;
  private String mMisses;


  public Game(String answer) {
  mAnswer = answer;
  mHits = "";
  mMisses = "";  
  }

  private char validateGuess(char letter) {
    if (! Character.isLetter(letter)) {
     throw new IllegalArgumentException("A letter is required"); 
    }
  }
  letter = Character.toLowerCase(letter);
  if (mMisses.indexOf(letter) >= 0 || (mHits.indexOf(letter) >= 0) {
    throw new IllegalArgumentException(letter + "has already been guessed");
    return letter;
      }
  public boolean applyGuess(String letter) {
    if (letters.length() == 0) {
      throw new IllegalArgumentException("No letter found");
    }

    return applyGuess(letters.charAt(0));
  }    

  public boolean applyGuess(char letter) {
    letter = validateGuess(letter);
    boolean isHit = mAnswer.indexOf(letter) >= 0;
    if (isHit) {
      mHits += letter;
    } else {
      mMisses += letter;
    }
}    
public String getCurrentProgress() {
  String progress = "";
  for (char letter: mAnswer.toCharArray()) {
   char display = '-';
    if (mHits.indexOf(letter) >= 0)
      display = letter;
  }
progress += display;

return progress;
}

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


public String getAnswer() {
  return mAnswer;
}

public boolean isSolved() {
  return getCurrentProgress().indexOf('-') == -1;
}```
Daniel Schwemmlein
Daniel Schwemmlein
4,960 Points
import java.io.Console;

public class Prompter {
  private Game mGame;

  public Prompter(Game game) {
    mGame = game;
  }

  public void play() {
    while (mGame.getRemainingTries > 0 && !mGame.isSolved()) {
      displayProgress();
      promptForGuess();
    }
    if (mGame.isSolved()) {
      System.out.printf("Congratulations you won with %d tries remaining!\n",mGame.getRemainingTries());
    } else {
      System.out.printf('Bummer the word was %s. :(\n",
                        mGame.getAnswer());
    }
  }  



  public boolean promptForGuess() {
   Console console = System.console();
   boolean isHit = false;
   boolean isValidGuess = false;
   while (! isValidGuess) { 
   String guessAsString = console.readLine("Enter a letter:  ");
   try {
     isHit = mGame.applyGuess(guessAsString);
     isValidGuess = true;
   } catch (IllegalArgumentException iae) {
     console.printf("%s. Please try again. \n", iae.getMessage());
}
   }
    return isHit;
  }
public void displayProgress() {
  System.out.printf("You have %d tries left to solve: %s\n",
                    mGame.getRemainingTries(),
                    mGame.getCurrentProgress());
}



} 
Jeremy Hutson
Jeremy Hutson
4,371 Points

while (mGame.getRemainingTries() > 0) {

Missing the ()