The assignment is to have the user put 3 words into a JOptionPane box, and then
ID: 3541167 • Letter: T
Question
The assignment is to have the user put 3 words into a JOptionPane box, and then have those words be displayed as an acronym. I have all of that down in the code below, but I also need the code to display an error message if exactly 3 words are not entered. How would I go about adding displaying that error message to the following code:
import javax.swing.*;
public class LetterAcronym
{
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();
one = Character.toString(str.charAt(0));
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
we will check if the string inputted is empty and also check that it is not a number
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.