0

Hello I am working on an assignment and I'm running into issues I was hoping for a little direction...

The purpose is to have user input a phrase and create an acronym out of that phrase. Anything over three words will be ignored.

I'm having issues with the acronym part, I am able to get the first character and figured that I would loop through the user input and grab the character after a space, but that is not working. All I am getting is the first character, which is obvious because I grab that first, but I can't figure out how to "save" the other two characters. Any help is greatly appreciated.

*********UPDATE************************ So thanks to an answer below I have made progress with using the StringBuilder. But, now if I enter "Your Three Words" the Output is: YYYYYTYYYYYWYYYY Which is progress but I can't understand why it's repeating those first characters so many times?? I edited the code too. *********UPDATE*****************************

public class ThreeLetterAcronym {

public static void main(String[] args) {
    String threeWords;
    StringBuilder acronym = new StringBuilder();

    Scanner scan = new Scanner(System.in);

    System.out.println("Enter your three words: ");
    threeWords = scan.nextLine();

    for(int count = 0; count < threeWords.length(); count++) {

        acronym.append(threeWords.charAt(0));

            if(threeWords.charAt(count) == ' ') {   
                ++count;
                acronym.append(threeWords.charAt(count));

            }

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

}
}
NoobCoderChick
  • 617
  • 3
  • 21
  • 40

2 Answers2

0

You can't save the other characters because char is supposed to store only one character. You can use a StringBuilder in this case

  StringBuilder acronym = new StringBuilder();

Then in your loop simply replace it with

       String[] threeWordsArray = threeWords.split(" ");

        for(String word : threeWordsArray) {
            acronym.append( word.substring(0, 1) );
        }

**updated

  • Ok cool, I'll try that out. I was just reading in a book something similar using substring, is that something that would be good to use instead of stringBuilder or is StringBuilder the most appropriate thing to use? And what about StringBuffer, I'm very new to all this and just learning about those two. – NoobCoderChick Jun 25 '15 at 23:53
  • I tried out what you said and It's not working as expected. I'm getting output of the very first Character repeated 12 times. So I input "Your Three Words" and the output is "YYYYYYYYYYYY" – NoobCoderChick Jun 26 '15 at 00:07
  • I updated above if you could take a look I'd appreciate it. – NoobCoderChick Jun 26 '15 at 00:20
  • My answer is updated :) if you're going to work with string that will be updated such as this, you should use the StringBuilder, you can check this for difference between StringBuffer and StringBuilder http://stackoverflow.com/questions/355089/stringbuilder-and-stringbuffer – Ologho Cyril Paul Jun 26 '15 at 00:50
  • Thank you so much Ologho! I have a quick question for you if you don't mind. Is creating an Array the only efficient way to do this? Are there other ways to grab the first character of different words in a one String Variable? – NoobCoderChick Jun 26 '15 at 02:02
  • Of course you can grab the first character of one single variable without the array, array is necessary and efficient when working with multiple words. For a one string variable all you need is `oneStringVariable.substring(0,1);` and you get the first character – Ologho Cyril Paul Jun 26 '15 at 08:02
0

You store the character at the current index in space:

char space = threeWords.charAt(count);

Then you compare the value of space with the integer value 3:

if(space < 3)

This will almost certainly never be true. You are asking for the numeric value of a character. Assuming it is a letter it will be at least 65. I suspect that your intention is to store something different in the variable space.

Tony Tuttle
  • 612
  • 7
  • 23
  • Oh, yeah you're right, I was trying to make space equal a space in the threeWords user input and stop at 3 spaces. Which isn't even important as far as what I'm having trouble with, is capturing more than just the first character in the inputed string, I'm fairly certain I can figure out how to get it to stop after three words...I think so at least lol. – NoobCoderChick Jun 25 '15 at 23:50