Home Java Java-tips Io 10file Example - Read Words

Ask Questions?

View Latest Questions

Advertisement


 
 

Example - Read Words
Posted on: July 26, 2006 at 12:00 AM
The program below reads a text file and lists the words alphabetically.

Java Notes: Example - Read Words

The program below reads a text file and lists the words alphabetically.

GUI interface and model

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
// File   : readwords/ReadWordsGUI.java
// Purpose: Read a file and display all words in it.
// Author : Fred Swartz
// Date   : 2005-03-10

package readwords;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

class ReadWordsGUI extends JPanel {
    
    //... Instance variables
    JTextArea    m_wordListTA  = new JTextArea(25, 15);
    JFileChooser m_fileChooser = new JFileChooser();
    
    ArrayList<String> m_words = new ArrayList<String>();
    
    //======================================================== constructor
    ReadWordsGUI() {
        JButton openButton = new JButton("Open");
        
        //... Add listeners
        openButton.addActionListener(new OpenAction());
        
        //... Layout components
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.add(openButton);
        this.add(new JScrollPane(m_wordListTA));
    }
    
    /////////////////////////////////////////////////// inner listener class
    class OpenAction implements ActionListener {
        public void actionPerformed(ActionEvent ae) {
            int retval = m_fileChooser.showOpenDialog(ReadWordsGUI.this);
            if (retval == JFileChooser.APPROVE_OPTION) {
                File file = m_fileChooser.getSelectedFile();
                try {
                    Scanner wordScanner = new Scanner(file);
                    wordScanner.useDelimiter("[^A-Za-z]+");
                    while (wordScanner.hasNext()) {
                        m_words.add(wordScanner.next());
                    }
                    
                    //... Sort the words alphabetically.
                    Collections.sort(m_words);
                    
                    //... Clear the text area and add words.
                    m_wordListTA.setText("");
                    for (String w : m_words) {
                        m_wordListTA.append(w);
                        m_wordListTA.append("\n");
                    }
                } catch (FileNotFoundException fnfex) {
                    System.out.println("Impossible?");
                }
            }
        }
    }
}

The standard main program

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
// File   : readwords/ReadWord.java
// Purpose: Read a file and display all words in it.
// Author : Fred Swartz
// Date   : 2005-03-10

package readwords;

import javax.swing.*;

public class ReadWords {
    public static void main(String[] args) {
        JFrame window = new JFrame("Read Words");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setContentPane(new ReadWordsGUI());
        window.pack();
        window.setVisible(true);
    }
}

Programming Problem

Sort buttons. Add two buttons to this user interface. One of them will sort the words alphabetically (as is currently done), and the other will sort the words by length. The GUI will then have three buttons and the text area. You might want to choose a different layout.

Comparator. Because Strings have a natural alphabetic sorting order, the Collections.sort() method doesn't need a Comparator for alphabetizing words. Sorting by word length will require writing a simple Comparator. Implementing the Comparator interface means defining the int compare method which compares two objects and returns a value less than zero if the first is less than the second, zero if they are equal, and a number greater than zero if the second is greater than the first.

Loops. The current code uses the Java 5 for loop for going over the list of words and adding them to the text area. The button listeners will have to do something similar. However, for practice, write each of the loops using a different technique (one using an index, and one using an Iterator).

Copyleft 2005 Fred Swartz - Program code placed in the public domain.
Advertisement


DMCA.com