Java Utililty Methods Console Prompt

List of utility methods to do Console Prompt

Description

The list of methods to do Console Prompt are organized into topic(s).

Method

Stringprompt(String message)
Prompts the user with a given message and returns their response.
System.out.print(message + ": ");
return scan.nextLine();
Stringprompt(String message, String defaults)
Prompt user for input, providing a default input.
System.out.print(message + " [" + defaults + "]: ");
String line = in.nextLine().trim();
return line.equals("") ? defaults : line;
Stringprompt(String output)
prompt
System.out.print(output);
Scanner input = new Scanner(System.in, "utf-8");
String line = input.nextLine();
input.close();
return line;
Stringprompt(String prompt)
Shorthand for the scanner creation, the posing of the question, and the getting of the response.
System.out.print(prompt + "\t");
return scan.nextLine();
voidpromptEnterKey()
Pause execution and allow the user to continue by pressing ENTER.
System.out.println("An Event has occured! Press \"ENTER\" to continue...");
scanner.nextLine();
StringpromptHidden(String message)
Prompot user for input, hiding the input if possible.
if (System.console() != null) {
    System.out.print(message + ": ");
    char[] line = System.console().readPassword();
    return new String(line);
return prompt(message);
voidpromptPressEnterToExit()
Prompts the user to press the enter key to exit the JVM process.
System.err.println("Press <enter> to exit...");
new Scanner(System.in).nextLine();
StringpromptRequired(String message)
Prompt user for input, requiring non-empty input.
System.out.print(message + ": ");
String line = in.nextLine().trim();
return line.equals("") ? promptRequired(message) : line;