Java Console Prompt promptHidden(String message)

Here you can find the source of promptHidden(String message)

Description

Prompot user for input, hiding the input if possible.

License

Open Source License

Parameter

Parameter Description
message a parameter

Return

a line of user's input

Declaration

public static String promptHidden(String message) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Scanner;

public class Main {
    private static Scanner in = new Scanner(System.in);

    /**//w  w w . jav a2s.c  o  m
     * Prompot user for input, hiding the input if possible. Useful for password input.
     * @param message
     * @return a line of user's input
     */
    public static String promptHidden(String message) {
        if (System.console() != null) {
            System.out.print(message + ": ");
            char[] line = System.console().readPassword();
            return new String(line);
        }
        return prompt(message);
    }

    /**
     * Prompt user for input.
     * @param message
     * @return a line of user's input
     */
    public static String prompt(String message) {
        System.out.print(message + ": ");
        return in.nextLine().trim();
    }

    /**
     * Prompt user for input, providing a default input.
     * @param message
     * @param defaults
     * @return a line of user's input
     */
    public static String prompt(String message, String defaults) {
        System.out.print(message + " [" + defaults + "]: ");
        String line = in.nextLine().trim();
        return line.equals("") ? defaults : line;
    }
}

Related

  1. prompt(String message)
  2. prompt(String message, String defaults)
  3. prompt(String output)
  4. prompt(String prompt)
  5. promptEnterKey()
  6. promptPressEnterToExit()
  7. promptRequired(String message)