0

How do I get the input on the same line as my command line in a Java application:

I currently have

private static String input(String msg) {
    Log.log(Level.INFO, "" + msg + ": ");
    return null;
}

public void listen() {
    while (true) {
        // Display input message
        input("\ntest-cli");

        // Gather user input
        String rawInput = scanner.nextLine();

        // Check if the users wishes to quit
        if (rawInput.equals("quit") || rawInput.equals("exit")) {
            break;
        }

        // Gather the raw arguments entered by the user
        String[] rawArgs = rawInput.split("\\s+");

        // Check any command or argument was entered
        if (!rawArgs[0].equals("")) {

            /* Check if debug mode is false, if so then run command in try catch to avoid crashing the application
            if an error is thrown. Otherwise run the command with no safety net */

            if (!debugMode) {
                try {
                    call(rawArgs);
                } catch (ArrayIndexOutOfBoundsException e) {
                    print("Command couldn't execute, perhaps not enough arguments? Try: " + rawArgs[0] + " help");
                } catch (Exception e) {
                    print("Command failed to execute.");
                }
            } else {
                call(rawArgs);
            }
        }
    }
}

static void print(String msg) {
    Log.log(Level.INFO, msg);
}

which outputs:

test-cli: 
help

How do I get it to look like this (rawInput same line as input)

test-cli> help
methuselah
  • 12,766
  • 47
  • 165
  • 315

0 Answers0