Hi I need help creating a word count file, the file should print the name and number of times the words were counted in a file. I need to use the JFileChooser and the results should be printed in the resultArea of the JTextfield.

example:

and 33
use 24

Here is what I have so far, my buttons arent working.

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Katherine Prosser
 */
public class WordCountFrame extends JFrame

{
    private Map myMap;
   
    private BufferedReader reader;
    private File file;

    private static final int FRAME_WIDTH = 300;
    private static final int FRAME_HEIGHT = 250;
    private static final int AREA_ROWS = 12;
    private static final int AREA_COLUMNS = 12;

    private JPanel controlPanel, displayPanel;
    private JButton startButton, exitButton;
    private JScrollPane displayScrollPane;
    private JTextArea DisplayArea;
    private final JTextArea resultArea;
    private JTextField textField;
    private String text;

    /**
     * Create a WordCount object that will be able to read
     * from a file selected using standard java FileChooser.
     */

    public WordCountFrame()
    {
    resultArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);
    resultArea.setEditable(false);

    createControlPanel();
    createControlPanel();

     setSize(FRAME_WIDTH,FRAME_HEIGHT);
     setLocation(FRAME_WIDTH/4, FRAME_HEIGHT/4 );

 }
    private void createDisplayPanel()
{
displayPanel = new JPanel();
displayPanel.add(controlPanel);
displayPanel.add(startButton);
displayPanel.add(exitButton);
add(displayPanel,BorderLayout.NORTH);
//create the textarea
//create the scrollpane with the textarea
//add the scrollpane to the panel
//add the panel to the frame
final int FIELD_WIDTH = 12;
textField = new JTextField(FIELD_WIDTH);
textField.setText("");
JScrollPane scrollPane = new JScrollPane(resultArea);
add(controlPanel, BorderLayout.SOUTH);
add(scrollPane);
add(scrollPane, BorderLayout.CENTER);
}

 private void createControlPanel()
{
  //create the panel
    controlPanel = new JPanel();
    controlPanel.setLayout(new GridLayout(1,4));
    //create the buttons
    startButton = new JButton("start");
    exitButton = new JButton ("exit");
   //add the buttons to the panel
    //add the panel to the frame
    controlPanel.add(startButton);
    controlPanel.add(exitButton);
    add(controlPanel,BorderLayout.NORTH);
    {
        startButton.addActionListener(new ActionListener()
    {
                private JFileChooser chooser;
                private int retval;
                private File file;
   public void actionPerformed(ActionEvent evt)
    {
        
    	if (chooser == null){
    		chooser = new JFileChooser(".");
    	}
	try{
	    
	    if (retval == JFileChooser.APPROVE_OPTION) {
		file = chooser.getSelectedFile();
		reader = new BufferedReader(
		               new FileReader(file.getPath()));
	    }
        }
	catch(Exception event){
	    throw new RuntimeException("file open problem in WordReader");
           }
       }
       
 
    /**
     * Read file selected at construction time and store words and
     * line numbers internally for subsequent reporting by print.
     *
     * @throws IOexception of reading file fails
     */
    void read() throws IOException
    {
	myMap = new TreeMap();
	String line;
	int linecount = 0;
	while ((line = reader.readLine()) != null) {
	    linecount++;

	    StringTokenizer tokenizer = new StringTokenizer(line);
	    while (tokenizer.hasMoreTokens()) {
		String word = tokenizer.nextToken();

		Set lineset = (Set) myMap.get(word);
		if (lineset != null) {
		    lineset.add(new Integer(linecount));
		}
		else {
		    lineset = new TreeSet();
		    lineset.add(new Integer(linecount));
		    myMap.put(word,lineset);
		}
	    }
	}
    }

    /**
     * Print list of words with line numbers as obtained
     * by most recent call to read.
     *
     */

    public void print()
    {
	Iterator allKeys = myMap.keySet().iterator();      // each word

	while (allKeys.hasNext()) {

	    Object key =  allKeys.next();

	    resultArea.append(key + "\t");
	    Iterator lines = ((Set) myMap.get(key)).iterator();
	    while (lines.hasNext()) {
		resultArea.append(lines.next() + " ");
	    }
	    
	}
       }
        });

Any help is appreciated..

Recommended Answers

All 3 Replies

The end of the program's code is missing.

Could you explain what my buttons arent working means

I got the exit button to workwhen the program starts but when I hit the start button to do a word count on a document, it doesnt open and I get alot of errors. Here is the modifed version.
Thanks,

class WordCountListener implements ActionListener
{
                private JFileChooser chooser;
                private int retval;
                private File file;
                private BufferedReader reader;
                private TreeMap Map;

public void actionPerformed(ActionEvent event)
        //quit Button
{
if (event.getSource() == exitButton)
{
System.exit(0);
}
 else
if (event.getSource ()== startButton)
{
   if (chooser == null){
    		chooser = new JFileChooser(".");
    	}
	try{

	    if (retval == JFileChooser.APPROVE_OPTION) {
		file = chooser.getSelectedFile();
		reader = new BufferedReader(
		               new FileReader(file.getPath()));
            }
    }

	catch(Exception evt){
	    throw new RuntimeException("file open problem in WordReader");
    }
    }
    }



    /**
     * Read file selected at construction time and store words and
     * line numbers internally for subsequent reporting by print.
     *
     * @throws IOexception of reading file fails
     */
    void read() throws IOException
    {
	Map = new TreeMap();
	String line;
	int linecount = 0;
	while ((line = reader.readLine()) != null) {
	    linecount++;

	    StringTokenizer tokenizer = new StringTokenizer(line);
	    while (tokenizer.hasMoreTokens()) {
		String word = tokenizer.nextToken();

		Set lineset = (Set) Map.get(word);
		if (lineset != null) {
		    lineset.add(new Integer(linecount));
		}
		else {
		    lineset = new TreeSet();
		    lineset.add(new Integer(linecount));
		    Map.put(word,lineset);
		}
	    }
	}
    }

    /**
     * Print list of words with line numbers as obtained
     * by most recent call to read.
     *
     */

    public void print()
    {
	Iterator allKeys = Map.keySet().iterator();      // each word

	while (allKeys.hasNext()) {

	    Object key =  allKeys.next();

	    resultArea.append(key + "\t");
	    Iterator lines = ((Set) Map.get(key)).iterator();
	    while (lines.hasNext()) {
		resultArea.append(lines.next() + " ");
	    }

	}
       }


}
//create the inheritance to each button
        WordCountListener buttListener = new WordCountListener();

         //create the buttons to the button panel
         exitButton.addActionListener(buttListener);
         startButton.addActionListener(buttListener);

    }
        }
        }

What can I do with a code fragment? I need a program that will compile and execute to see what the problem is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.