1

I'm creating a text-based RPG for my AP computer Science final Project and I want to make it so that at any point the player can type "help" and it will display a message in the console. The only way I can think to do it would be to use the Scanner import and do something like:

    String help = scan.nextLine();
    switch(help) {
        case help:
        System.out.println("help message");
        break;
}

I've been teaching myself Java for the project I'm very new and I'm aware this is probably an extremely inefficient method for this, not to mention it would only work at 1 point. So if someone could point me in the right direction I'd be eternally grateful. Also: I have searched for the answer before submitting this post but I couldn't find one that described how to print it to the console throughout the game.

  • Can you show how you're processing input in general? You might want to delegate calling `scan.nextLine()` to a separate method that would account for needing help. – ostrichofevil Mar 18 '17 at 03:05
  • Looking at the answers below that is starting to make more and more sense. Up until now I've been setting a variable to scan.nextLine(); then making a decision with a switch statement. – Andrew Young Mar 18 '17 at 03:26
  • 1
    The thing is that you don't just want to break. You want to call the method again, once you have printed the help. – ostrichofevil Mar 18 '17 at 03:28

3 Answers3

0

Use a giant if..elif..then statement to capture commands, not a switch statement. Do not forget to use the String.equals() method to compare strings, too! See why here.

String cmd = scan.nextLine();
if(cmd.equals("help")) {
    System.out.println("help message");
} else if(cmd.equals("move")) {
    System.out.println("move");
} else {
    System.out.println("I'm sorry, I did not understand that command :(")
}
Community
  • 1
  • 1
Mayazcherquoi
  • 474
  • 1
  • 4
  • 12
  • Why not a `switch` statement? The answers to [this question](http://stackoverflow.com/questions/427760/when-to-use-if-else-if-else-over-switch-statments-and-vice-versa) say that it would be a good fit for this situation. – MasterBlaster Mar 18 '17 at 03:09
  • Ah, TIL that you can now [use strings in switch statements](http://stackoverflow.com/questions/338206/why-cant-i-switch-on-a-string). I'll still leave my answer here as an alternative way to solve the problem, though, and let you (or others) answer appropriately. :-) – Mayazcherquoi Mar 18 '17 at 03:12
  • Wouldn't it be better to create a method outside of main that performs this instead of using (what looks like to me) a variable? – Andrew Young Mar 18 '17 at 03:45
0

You could create a method that handles asking for help, which you will always use instead of scan.nextLine():

public static String getNextLine(Scanner scan) {
    String str = scan.nextLine(); //Get the next line
    if (str.equals("help") { //Check whether the input is "help"
        System.out.println(/* help */); //if the input is "help", print your help text
        return getNextLine(scan); //try again.
    else {
        return str; //return the inputted string
    }
}

Now, wherever you would use scan.nextLine, use getNextLine(scan), instead. This way, you will automatically account for when the user inputs "help". (Just a tip, you might want to use equalsIgnoreCase instead of equals so that if the user types, say, "Help", your code still works.)

ostrichofevil
  • 749
  • 7
  • 19
  • So i would want to call getNextLine(scan) at every instance I would normally use scan.nextLine();? Could I create a method like this that creates all the inputs like "help", "attack", "run", "rest" etc... – Andrew Young Mar 18 '17 at 03:36
  • public static String getNextLine(Scanner scan) { String str = scan.nextLine(); if (str.equalsIgnoreCase("help")) { System.out.println("help msg"); return getNextLine(scan); } else if (str.equalsIgnoreCase("attack")) { etc – Andrew Young Mar 18 '17 at 03:36
  • @AndrewYoung Yes to the first question; simply substitute all uses of scan.nextLine(). I personally would not create methods for the rest, just use if and else if statements. – ostrichofevil Mar 18 '17 at 03:40
0

I'd use the Apache Commons CommandLine class in addition to the HelperFormatter:

private static final String HELP = "help";
private static final String SOME_OTHER_TOGGLE = "toggle";

/**
 * See --help for command line options.
 */
public static void main(String[] args) throws ParseException {
    Options options = buildOptions();
    CommandLineParser parser = new DefaultParser();
    CommandLine line = parser.parse(options, args);

    if (line.hasOption(HELP)) {
        printHelp(options);
    }
}

private static void printHelp(Options options) {
    String header = "Do something useful with an input file\n\n";
    String footer = "\nPlease report issues at http://example.com/issues";

    HelpFormatter formatter = new HelpFormatter();
    formatter.printHelp("myapp", header, options, footer, true);
}

private static Options buildOptions() {
    Options options = new Options();
    options.addOption(OptionBuilder
        .withLongOpt(SOME_OTHER_TOGGLE)
        .withDescription("toggle the foo widget in the bar way")
        .create()
    );
    options.addOption(OptionBuilder
        .withLongOpt(HELP)
        .withDescription("show the help")
        .create()
    );

    // ... more options ...

    return options;
}
Jameson
  • 6,400
  • 6
  • 32
  • 53