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
Jason Twichell
1,540 PointsQuestion about method parameter (letter) in Java Basics
I've been following along with Java Basics course and I understand the architecture and logic of the program. However, I'm confused about where this (letter) parameter comes from and what it means to pass a method a parameter. As far as I can see, we never declared or assigned letter a type, so how does the method know what is assigned to letter. This may be a stupid question, I apologize in advance.
Here is the code for those who have not done the course:
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("You've already guess this!");
}
return letter;
}
public boolean applyGuess(char letter){
letter = validateGuess(letter);
boolean isHit = mAnswer.indexOf(letter) >= 0;
if(isHit) {
mHits += letter;
} else{
mMisses += letter;
}
return isHit;
}
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();
}
}
3 Answers
Nick Hooper
7,188 PointsLetter is the character that the user types in as a guess torwards the word of hangman.
Letter is defined in the prompter from guessAsString. It then gets passed to applyGuess, then validateGuess.
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 boolean applyGuess(char letter) {
letter = validateGuess(letter);
boolean isHit = mAnswer.indexOf(letter) >= 0;
if (isHit) {
mHits += letter;
} else {
mMisses += letter;
}
return isHit;
}
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;
}
Jason Twichell
1,540 PointsThank you for your reply. I see now that we're setting letter to the result of the input after its been validated...this ensures that letter is intact a letter and not some other character. Does this mean that methods can define variables within their parameters?
Nick Hooper
7,188 PointsYes, and is defined dynamically by the specified data type that you throw at it. However other methods cannot access this variables and you must pass it down through a pre-defined variable
The boolean accepts a String here, and calls it "letters". public boolean applyGuess(String letters)
We enter a letter and its saved as the string "guessAsString". String guessAsString = console.readLine("Enter a letter: ");
Then that gets passed here into applyguess. Since "guessAsString" is a string, we can put it in here.
mGame.applyGuess(guessAsString) //This is the boolean applyGuess in action
so guessAsString gets turned into letters
Jason Twichell
1,540 PointsThank you so much, you've helped me tremendously.
Nick Hooper
7,188 PointsYou will start to understand more about passing them as you get closer to the end.