Answer to Question #181320 in Java | JSP | JSF for Bill

Question #181320

Create a program that uses a dialog box to ask “Enter a sentence.”, take the user input, and then store the user input in a string variable.


Use the “split()” method to break the address into an array of String type. Then, use a “for..in” (also known as “for..each”)loop to display every word in a one-word-per-line manner in one dialog box.


Then, use: (1) the “toUpperCase()” method to convert every character to uppercase, (2) the toCharArray() method to break the same string literal to an array of char type, and (3) a “for” loop to display every element of the char array in a one-character-per-line manner in another dialog box.


1
Expert's answer
2021-04-14T04:22:51-0400
import javax.swing.JOptionPane;
public class MyProg {
public static void main(String[] args) {
String sentence = JOptionPane.showInputDialog(null, "Enter a sentence: ");
//use split method to split words
String str[] = sentence.split(" ");
// create string to display in Messagedialog box
String words = "";
//for each loop to str array
for (String str1 : str) {
words += str1 + "\n"; // \n to display per line
}
// display words per line
JOptionPane.showMessageDialog(null,words);
// convert each character to Uppercase
sentence = sentence.toUpperCase();
// convert each word to character array
char[]ch = sentence.toCharArray();
String chars = "";
// appends character to chars string to display in dialog box
for (int i = 0; i < ch.length; i++) {
chars += ch[i]+"\n";
}
JOptionPane.showMessageDialog(null,chars);
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog