hello everyone,
Ive been trying to alter strings and i am having a very difficult time altering the vlaue after i split them. i understand what i am doing when i split the input(String[] word = phrase.split(" ");), thanks to this site.. and how i am then able to alter the characters and positions such as "Character.toString(word[0].charAt(0)).toUpperCase()". However, anything further than this i am totally confused. For example. my homework requires me to retrieve the first letter of users 3 words and create an acronym. i have successfullly done this but wanted to display an error message if user enters less than or more than 3 words. i tried setting up some if and else statements but compiler slapped me and called me stupid. Also, once i split the words, is it possible to to capture the first letter for all the users input whether its 3 or 30 words?

import javax.swing.JOptionPane;
import java.lang.*;
public class ThreeLetterAcronym
    public static void main(String []args)
            String phrase = JOptionPane.showInputDialog(null, "Please enter 3 words for me to program.");
            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, "You chose: " + phrase + " . The acronym for the following is " + acronym);
        }//checked on oracle's tutorials for splitting strings. this way seems way easier but having difficult setting error for anything above 3 words.


our class has not yet tackled splitting phrases,(which is why i am struggling but this way seems much more clean and simple) but if you could point me in right direction to research would be greatly appreciated.

Recommended Answers

All 2 Replies

your array "word" contains one entry for each word in the input.
word.length tells you the nunber of entries in the array
you can test that value to be >= 3 in an if test

if that's not enough of a hint, get as far as you can and post your best attempt(s) here - someone will help.

Thank you much! took some guessing and testing but I got it! appreciate your time=)

Be a part of the DaniWeb community

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