Java Scanner Usage confirm(String message)

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

Description

Ask user to confirm an action.

License

Open Source License

Parameter

Parameter Description
message a parameter

Declaration

public static boolean confirm(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);

    /**/*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");
    }
}

Related

  1. cleanUpEmptyLinesAndIndent(String input)
  2. close()
  3. common(Scanner in)
  4. computeSumTable(Scanner in, short n)
  5. confirm(String message)
  6. confirmAction(String warning)
  7. countFileLines(Scanner fin)
  8. createKeyboardScanner()
  9. createScannerForString(String s)