Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to add these to the following code: Use a JOptionPane to get the user\'s

ID: 3655785 • Letter: I

Question

I need to add these to the following code: Use a JOptionPane to get the user's phrase. If the phrase doesn't contain three words, display an error message. Here is the code I have so far: import javax.swing.*; public class ThreeLetterAcronym { public static void main(String[] args) { String str = JOptionPane.showInputDialog(null,"Please enter three words"); String; String two = ""; String three = ""; String acronym; char c; int counter = 0; int stringLength = str.length(); counter = 1; for(int i = 1; i < stringLength; i++) { c=str.charAt (i); if(Character.isWhitespace(c)) { if(counter == 1) { two = Character.toString(str.charAt (i+1)); counter ++; } else if(counter == 2) { three = Character.toString(str.charAt(i+1)); counter ++; break; } } } acronym = one.toUpperCase() + two.toUpperCase() + three.toUpperCase(); JOptionPane.showMessageDialog(null, "The Original phrase was: "+ str + " Three letter acronym is " + acronym); } }

Explanation / Answer

//use String.split method is relatively easier //create file ThreeLetterAcronym.java and run it :P import javax.swing.*; public class ThreeLetterAcronym { public static void main(String[] args) { String str = JOptionPane.showInputDialog(null, "Please enter three words"); String[] words = str.split(" "); if(words.length != 3){ JOptionPane.showMessageDialog(null, "Cannot find three words!", "Error", JOptionPane.ERROR_MESSAGE); }else{ String acronym = "" + words[0].charAt(0) + words[1].charAt(0) + words[2].charAt(0); JOptionPane.showMessageDialog(null, "The Original phrase was: " + str + " Three letter acronym is " + acronym.toUpperCase(), "Result", JOptionPane.INFORMATION_MESSAGE); } } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote