Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java: Example - SortWords3.java

This program takes a string in one text field, breaks it into words, sorts the words, and makes a string of these sorted words, which is then displayed in another text field.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

// SortWords3: sorts words that that user enters.
//   31 Aug 1998, Fred Swartz
// Updated: 2000-05-18 to Java 2
// Updated: 2002-04-29 (Sicily) to main
// Updated: 2002-04-30 use Collections data structures

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.*;     // For ActionListener ...
import javax.swing.*;        // For components
import java.util.*;          // For StringTokenizer

///////////////////////////////////////////////////////////////// SortWords3
public class SortWords3 extends JFrame {
    //============================================================ variables
    JTextField inField;   // get the input from here
    JTextField outField;  // put the output here

    //========================================================== constructor
    public SortWords3() {
        inField = new JTextField(30);   // use this for input
        JButton sortWordsButton = new JButton("Sort Words");
        sortWordsButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                List words = stringToList(inField.getText());
                Collections.sort(words);
                outField.setText(listToString(words, ", "));
            }
          });

        outField = new JTextField(30);  // use this for output
        outField.setEditable(false);    // don't let the user change it

        // Layout the fields
        Container content = this.getContentPane();
        content.setLayout(new FlowLayout());
        content.add(new JLabel("Input"));
        content.add(inField);
        content.add(sortWordsButton);
        content.add(new JLabel("Sorted output"));
        content.add(outField);

        this.setTitle("SortWords3");
        this.pack();
    }//end constructor

    
    //========================================================= stringToList
    // Put all the "words" in a string into an array.
    List stringToList(String wordString) {
        ArrayList result = new ArrayList(20);
        StringTokenizer st = new StringTokenizer(wordString);
        while (st.hasMoreTokens()) { //--- Loop, getting each token
            result.add(st.nextToken());
        }
        return result;
    }//end stringToArray

    
    //========================================================= listToString
    // Convert list of strings to one string with 'separator' between each.
    String listToString(List a, String separator) {
        StringBuffer result = new StringBuffer(40);
        for (Iterator iter = a.iterator(); iter.hasNext(); ) {
            result.append(iter.next() + separator);
        }
        return result.toString();
    }//endmethod arrayToString
    
    
    //================================================================= main
    public static void main(String[] args) {
        JFrame window = new SortWords3();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
    }//end main
}//endclass SortWords3

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.