Java Console Prompt prompt(String message, String defaults)

Here you can find the source of prompt(String message, String defaults)

Description

Prompt user for input, providing a default input.

License

Open Source License

Parameter

Parameter Description
message a parameter
defaults a parameter

Return

a line of user's input

Declaration

public static String prompt(String message, String defaults) 

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);

    /**//from   w w w  .jav a  2 s  . c o  m
     * 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 output)
  3. prompt(String prompt)
  4. promptEnterKey()
  5. promptHidden(String message)