Java Java Objects Delivering the MVP Arrays and Command Line Arguments

Joshua Thao
Joshua Thao
5,485 Points

How 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
Allan Clark
10,801 Points

All 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

Sandy Woods, Jr
PRO
Sandy Woods, Jr
Pro Student 1,929 Points

What'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
PRO
Sandy Woods, Jr
Pro Student 1,929 Points

public 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;
}

}