Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


hashihi raja
java
1 Answer(s)      3 years and 8 months ago
Posted in : Java Beginners


Write a complete Java program that takes in any sentence from the user through a text field and splits the sentence into individual words. Next, append all the words from the sentence to a text area in reverse order. Finally, display the total number of words that have gone through this process in another text field. Provide a button labeled ‚??EXECUTE‚?? to run this process, and another button labeled ‚??CLEAR‚?? to clear the sentence input text field. Your main program should be separated in another class.


Advertisement
View Answers

March 7, 2011 at 3:44 PM


Java Split String

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Split
{
    Split(){    
    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("Execute");
    JButton b1=new JButton("Clear");
    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));
        }
    });
    b1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            text1.setText("");
            text2.setText("");
            area.setText("");
        }
    });
        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(10,150,100,20);
        b1.setBounds(150,150,100,20);

        f.add(label1);
        f.add(text1);
        f.add(label2);
        f.add(area);
        f.add(label3);
        f.add(text2);
        f.add(b);
        f.add(b1);
        f.setVisible(true);
        f.setSize(400,250);
    }
}
    class SplitString{
    public static void main(String[] args) {
        new Split();
    }
}


Related Tutorials/Questions & Answers:
java
java  diff bt core java and java
java
java  what is java
Advertisements
Java
Java   Whether Java is pure object oriented Language
java
java  different between java & core java
java
java  explain technologies are used in java now days and structure java
java
java   why iterator in java if we for loop
JAVA
JAVA  how the name came for java language as "JAVA
java
java  what is java reflection
java
java  is java open source
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
java  write a java program using filenotfoundexception
Java
Java  how to draw class diagrams in java
java
java   difference between class and interface
java
java  why multiple inheritance is not possible in java
java
java  explain object oriented concept in java
java
java  is java purely object oriented language
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  what are JAVA applications development tools
java
java  how use java method
java
java  send me java interview questions
java
java  write a program in java to acess the email
java
java  Write a java code to print "ABABBABCABABBA
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  give a simple example for inheritance in java
java
java  give a simple example for inheritance in java
Java
Java  how to do java in command prompt
java
java  Does java allows multiline comments
 

 

 

DMCA.com