//8. Three-letter acronyms are common in the business world. For example, in Jav
ID: 665535 • Letter: #
Question
//8. Three-letter acronyms are common in the business world. For example, in Java you
// use the IDE (Integrated Development Environment) in the JDK (Java Development
// Kit) to write programs used by the JVM (Java Virtual Machine) that you might send
// over a LAN (local area network). Programmers even use the acronym TLA to stand
// for three-letter acronym. Write a program that allows a user to enter three words, and
// display the appropriate three-letter acronym in all uppercase letters. If the user enters
// more than three words, ignore the extra words. Figure 7-22 shows a typical execution.
// Save the file as ThreeLetterAcronym.java.
//Exercise 8 Additional Requirements
// Use a JOptionPane to get the user's phrase.
// If the phrase doesn't contain three words, display an error message.
import javax.swing.*;
public class ThreeLetterAcronym{
public static void main(String[]args){
String tLA;
tLA = JOptionPane.showInputDialog(null, "Enter a three word phrase to recieve a three-letter acronym");
String[] letters = tLA.split(" ");
char firstWord = letters[0].charAt(0);
char secondWord = letters[1].charAt(0);
char thirdWord = letters[2].charAt(0);
JOptionPane.showMessageDialog(null, "Your three-letter Acronym is: "+firstWord+secondWord+thirdWord);
}
}
My teacher wants a error message if you enter more than three words. I tried doing an if statement for the extra word and it gives me a n error message about char not compatible. whats the easiest way to make an error message for an extra word in the three letter acronym?
Explanation / Answer
You implement this code , i tried to make as short as possible. in this code less number of lines .
public static void main(String[] args) throws IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
String acronym = "";
System.out.print("Enter the word: ");
String input = rd.readLine();
String[] words = input.split(" ");
for (int i = 0; i < words.length; i++) {
if (i == 3) {
break;
}
if(words[i].length() > 0){
acronym += words[i].charAt(0);
}
}
System.out.println(acronym.toUpperCase()...
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.