Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force a stringBuilder to an upper case

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
like image 436
NoobCoderChick Avatar asked Sep 13 '26 23:09

NoobCoderChick


1 Answers

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());
like image 74
Eran Avatar answered Sep 15 '26 12:09

Eran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!