Here you can find the source of confirm(String message)
Parameter | Description |
---|---|
message | a parameter |
public static boolean confirm(String message)
//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 ww w .ja v a 2 s . co m*/ * Ask user to confirm an action. * @param message */ public static boolean confirm(String message) { System.out.print(message + " (yes/no): "); String answer = in.nextLine().toLowerCase().trim(); if (answer.equals("yes") || answer.equals("y")) return true; else if (answer.equals("no") || answer.equals("n")) return false; return confirm(message); } /** * Ask user to confirm an action, providing a default choice. * @param message * @param defaults */ public static boolean confirm(String message, String defaults) { System.out.print(message + " (" + (defaults.equalsIgnoreCase("yes") ? "YES" : "yes") + "/" + (defaults.equalsIgnoreCase("no") ? "NO" : "no") + "): "); String answer = in.nextLine().toLowerCase().trim(); if (answer.equals("yes") || answer.equals("y")) return true; else if (answer.equals("no") || answer.equals("n")) return false; return defaults.equalsIgnoreCase("yes"); } }