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 trialJoshua Thao
6,439 PointsHow do we set the "Answer" in an IDE? I have Intellij and if I run my code it just says answer is required.
import java.sql.SQLOutput;
import static java.lang.System.*;
public class Hangman {
public static void main(String[] args) {
if(args.length == 0){ // checking to see if we put an answer for the game
out.println("Usage: java Hangman <answer>");
err.println("answer is required!");
System.exit(1);
}
Game game = new Game(args[0]);
Prompter prompter = new Prompter(game);
while (game.getRemainingTries() > 0 && !game.isWon()) {
prompter.displayProgress();
prompter.promptForGuess();
}
prompter.displayOutcome();
}
}
4 Answers
Allan Clark
10,810 PointsAll IDEs have a place where you can input command line arguments. I am not familiar with Intellij but this StackOverflow question should help.
https://stackoverflow.com/questions/2066307/how-do-you-input-commandline-argument-in-intellij-idea
Joshua Thao
6,439 PointsThankyou!
Sandy Woods, Jr
Courses Plus Student 2,834 PointsWhat's the "err" in you and its function? I'm getting the same error message compiling in IntelliJ Even after I read that article and got my code to run in, same message appears without allowing me to input a value.
Sandy Woods, Jr
Courses Plus Student 2,834 Pointspublic class Hangman{
public static void main(String[] args){ if(args.length == 0){ System.out.println("Usage: java Hangman <answer>"); System.out.println("answer is required"); System.exit(1); } Game game = new Game(args[0]); Prompter prompter = new Prompter(game); while(game.getRemainingTries() > 0 && !game.isWon()){ prompter.displayProgress(); prompter.promptForGuess(); } prompter.displayOutcome(); } }
import java.util.Scanner;
public 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.printf("%s. Please try again.%n", iae.getMessage()); } } while(!isAcceptable); return isHit; } public void displayProgress(){ System.out.printf("Try to %d to solve: %s%n", game.getRemainingTries(), game.getCurrentProgress()); } public void displayOutcome(){ if (game.isWon()) { System.out.printf("Congratulation! you won with %d tries remaining.%n", game.getRemainingTries()); } else { System.out.printf("Sorry, the answer was %s.%n", game.getAnswer()); }
} } public class Game { public static final int MAX_MISSES = 7; private String answer; private String hits; private String misses;
private char normalizeGuess(char letter){
if(! Character.isLetter(letter)){
throw new IllegalArgumentException("a letter is required");
}
letter = Character.toLowerCase(letter);
if (misses.indexOf(letter) != -1 || hits.indexOf(letter) != -1 ){
throw new IllegalArgumentException(
letter + " has already been guessed! Pick another letter.");
}
return letter;
}
public Game(String answer){
this.answer = answer.toLowerCase();
hits = "";
misses = "";
}
public String getAnswer(){
return answer;
}
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){
hits += letter;
}else{
misses += letter;
}
return isHit;
}
public int getRemainingTries(){
return MAX_MISSES - misses.length();
}
public String getCurrentProgress(){
String progress = "";
for(char letter: answer.toCharArray()){
char display = '-';
if(hits.indexOf(letter) != -1){
display = letter;
}
progress += display;
}
return progress;
}
public boolean isWon(){
return getCurrentProgress().indexOf('-') == -1;
}
}