Here you can find the source of prompt(String prompt)
Parameter | Description |
---|---|
prompt | the string posed to the user. |
static String prompt(String prompt)
//package com.java2s; import java.util.List; import java.util.Scanner; public class Main { private static Scanner scan = new Scanner(System.in); /** Shorthand for the scanner creation, the posing of the question, and the getting of the response. This version of * the prompt method will not return all responses in lower case. * @param prompt the string posed to the user. * @return the user's answer*/ static String prompt(String prompt) { System.out.print(prompt + "\t"); return scan.nextLine(); }//w w w. ja va2s . c om /** Sends data and requests that you sanitise it to avoid stupid errors. All responses will be in lower case. This * is the only way the data can be effectively sanitised. * @param prompt The question posed to the user. * @param acceptableAnswers A list of valid responses. * @return the user's answer, which is required to be of the list of valid responses */ static String prompt(String prompt, List<String> acceptableAnswers) { String response; while (true) { response = prompt(prompt).toLowerCase(); if (acceptableAnswers.contains(response)) break; else System.out.println("Please provide an acceptable answer."); } return response; } }