Force a stringBuilder to an upper case

NoobCoderChick picture NoobCoderChick · Jun 26, 2015 · Viewed 11k times · Source

I was wondering if there is anyway to use something like the toUpperCase for StringBuilder? Below is my code, I am trying to take user input of a phrase and turn it into an acronym. Someone helped me out by suggesting StringBuilder but I can't figure out if there is a way to make the acronym upper case. Any help is greatly appreciated.

public class ThreeLetterAcronym {

public static void main(String[] args) {
    String threeWords;
    int count = 0;
    int MAX = 3;
    char c;
    //create stringbuilder
    StringBuilder acronym = new StringBuilder();

    Scanner scan = new Scanner(System.in);

    //get user input for phrase
    System.out.println("Enter your three words: ");
    threeWords = scan.nextLine();

    //create an array to split phrase into seperate words.
    String[] threeWordsArray = threeWords.split(" ");

    //loop through user input and grab first char of each word.
    for(String word : threeWordsArray) {
        if(count < MAX) {
            acronym.append(word.substring(0, 1));
            ++count;

        }//end if
    }//end for  

    System.out.println("The acronym of the three words you entered is: " + acronym);
    }//end main
}//end class

Answer

Eran picture Eran · Jun 26, 2015

Just append upper case Strings to it :

acronym.append(word.substring(0, 1).toUpperCase())

or turn the String to upper case when getting the String from the StringBuilder :

System.out.println("The acronym of the three words you entered is: " + acronym.toString().toUpperCase());