
Write a Java program that takes in any sentence from the user through a text field and split the sentence into individual words. Next append all the words from the sentence to a text area in reversed order. Finally, display the total number of words that have gone through this process in another text field. Provide a button labelled â??RUNâ?? to run this process.

Java program that takes in any sentence from the user through a text field and split the sentence into individual words.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class SplitString
{
public static void main(String[] args)
{
JFrame f=new JFrame();
f.setLayout(null);
JLabel label1=new JLabel("String: ");
JLabel label2=new JLabel("Reversed String: ");
JLabel label3=new JLabel("Count Words: ");
final JTextField text1=new JTextField(20);
final JTextArea area=new JTextArea(5,20);
final JTextField text2=new JTextField(20);
JButton b=new JButton("Submit");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String str=text1.getText();
StringBuffer buffer=new StringBuffer();
String array[]=str.split(" ");
for(int i=array.length-1;i>=0;i--){
buffer.append(array[i]);
buffer.append(" ");
}
area.setText(buffer.toString());
text2.setText(Integer.toString(array.length));
}
});
label1.setBounds(10,10,100,20);
text1.setBounds(150,10,200,20);
label2.setBounds(10,40,100,20);
area.setBounds(150,40,200,60);
label3.setBounds(10,120,100,20);
text2.setBounds(150,120,200,20);
b.setBounds(150,150,80,20);
f.add(label1);
f.add(text1);
f.add(label2);
f.add(area);
f.add(label3);
f.add(text2);
f.add(b);
f.setVisible(true);
f.setSize(400,250);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.