Java - Write code to Return the provided text with its first letter in upper or lower case, depending on what is provided for the "upper" parameter.

Requirements

Write code to Return the provided text with its first letter in upper or lower case, depending on what is provided for the "upper" parameter.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String text = "book2s.com";
        boolean upper = true;
        System.out.println(firstLetterToCase(text, upper));
    }//  w ww  . j a  va  2s  . c o  m

    /**
     * Returns the provided text with its first letter in upper or lower case,
     * depending on what is provided for the "upper" parameter.
     * @param   text   A string value
     * @param   upper   Whether the first letter should be upper or lower case
     * @return   The text with its first letter in upper or lower case
     */
    public static String firstLetterToCase(String text, boolean upper) {
        char[] stringArray = text.toCharArray();

        if (text.length() >= 1) {
            if (upper) {
                stringArray[0] = Character.toUpperCase(stringArray[0]);
            } else {
                stringArray[0] = Character.toLowerCase(stringArray[0]);
            }
        }

        return new String(stringArray);
    }
}

Related Exercise