Formats a boolean into common words. - Java java.lang

Java examples for java.lang:boolean

Description

Formats a boolean into common words.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String guess = "java2s.com";
        boolean b = true;
        System.out.println(formatBoolean(guess, b));
    }//from w w  w.  ja v  a 2s  . co  m

    public static String formatBoolean(String t, String f, boolean b) {
        if (b)
            return t;
        else
            return f;
    }

    /**
     * Formats a boolean into common words.
     * Supported Words: YES, NO, TRUE, FALSE, ON, OFF
     *       ACCEPT, DENY, ACCEPTED, DENIED
     * If you were to pass in "yes" and true, it would return "yes"
     * If you were to pass in "accept" and false, it would return "deny"
     * 
     * @param guess The word to format the boolean into, can be any of the supported words
     * @param b The boolean to format
     * @return The formated boolean
     * @throws IllegalArgumentException if the guess is not supported
     * @see #formatBoolean(String, String, boolean)
     */
    public static String formatBoolean(String guess, boolean b) {
        if (guess.equalsIgnoreCase("YES") || guess.equalsIgnoreCase("NO"))
            return formatBoolean("yes", "no", b);
        else if (guess.equalsIgnoreCase("ON")
                || guess.equalsIgnoreCase("OFF"))
            return formatBoolean("on", "off", b);
        else if (guess.equalsIgnoreCase("TRUE")
                || guess.equalsIgnoreCase("FALSE"))
            return formatBoolean("true", "false", b);
        else if (guess.equalsIgnoreCase("ACCEPTED")
                || guess.equalsIgnoreCase("DENIED"))
            return formatBoolean("accepted", "denied", b);
        else if (guess.equalsIgnoreCase("ACCEPT")
                || guess.equalsIgnoreCase("DENY"))
            return formatBoolean("accept", "deny", b);
        else
            throw new IllegalArgumentException("This does not yet support "
                    + guess);

    }
}

Related Tutorials