Hi I'm in some serious need of help with a homework assignment. I have to write a program that takes a three word phrase from user input (through JOptionPane) and my code has to convert it to uppercase and then just take the first letter of the three words.

here is what I have so far, please help!!

import javax.swing.*;
public class ThreeLetterAcronym
{
    public static void main(String[] args)
  {
	 String phrase = JOptionPane.showInputDialog(null, 
	 "Please enter three words.");
	 String acronym;
	 char one, two, three;
	 int stringLength;                //stringLength is used for loop
	 int z;                           //z is reference to character placement
	 char c;                          //c is used for character value
	 stringLength = phrase.length();
	 one = phrase.charAt(0);
    
	   for(z = 0; z < stringLength; z++)
	   {
		  int a = 1;
	     c = phrase.charAt(z);
		  if(Character.isWhitespace(c)) 
		    if (a <= 1)
			   two = phrase.charAt(z+1);
			 else if(a <= 2)
			   three = phrase.charAt(z+1);
	   }
acronym = one + two + three;


	   JOptionPane.showMessageDialog(null, "Original words were: " +
		phrase + "\nThree letter acronym is " + one + two + three);
	 
  }
}

Recommended Answers

All 7 Replies

Call split("\\s+") on the string that results from calling toUpperCase(). Then, on each element of that resulting array, call charAt(0) on the result of calling trim() on that element.

I don't understand that terminology, would you mind showing me an example? thanks!

Call toUpperCase() on the string assigning the result back to the original string.
Call split("\\s+") on that String assigning the result to a new String[]
On each element of that array call trim() assigning the result back into the same element of the array.
Call charAt(0) on each element of the array to retreive the acronym characters.

I understood the first and fourth lines, but I can't use split or trim(we haven't learned them yet). Any other suggestions? thanks!

Why not? Does your instructor consider you incapable of reading the API docs for String?

I'm going to say yes. In our homework assignments we are encouraged to use what the book has taught us and I can't find anything about API docs for String, split(), or trim().

Member Avatar for ztini
public class Acronym {
	public static void main(String[] args) {
		String phrase = JOptionPane.showInputDialog(null, 
		 	"Please enter three words.");
		
		String[] word = phrase.split(" ");
		
		String acronym =
			Character.toString(word[0].charAt(0)).toUpperCase() + 
			Character.toString(word[1].charAt(0)).toUpperCase() + 
			Character.toString(word[2].charAt(0)).toUpperCase();
		
		JOptionPane.showMessageDialog(null, "Original words were: " +
				phrase + "\nThree letter acronym is " + acronym);
	}
}

The API can be tough to learn by. Here is Oracle's tutorial on Strings.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.