Develop an application that counts the number of words


Develop an application that counts the number of words, number of characters with spaces and without spaced. The program should display the text file statistics as follows:

File Name: sample.txt
Number of Words: 124
Number of characters without spaces: 1045
Number of characters with spaces: 1200

The application shall be a counsel compatible application-(needs a GUI)
here's what I have so far...
i'm missing both character counters and am not sure how to get files in my gui
__________________________________________________________________

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

//////////////////////////////////////////////////////// CountWords
public class CountWords extends JFrame {

//====================================================== fields
JTextField _fileNameTF = new JTextField(15);
JTextField _wordCountTF = new JTextField(4);
JFileChooser _fileChooser = new JFileChooser();

//================================================= constructor
CountWords() {
//... Create / set component characteristics.
_fileNameTF.setEditable(false);
_wordCountTF.setEditable(false);

//... Add listeners

//... Create content pane, layout components
JPanel content = new JPanel();
content.setLayout(new FlowLayout());
content.add(new JLabel("File:"));
content.add(_fileNameTF);
content.add(new JLabel("Word Count:"));
content.add(_wordCountTF);

//... Create menu elements (menubar, menu, menu item)
JMenuBar menubar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open...");
openItem.addActionListener(new OpenAction());

//... Assemble the menu
menubar.add(fileMenu);
fileMenu.add(openItem);

//... Set window characteristics
this.setJMenuBar(menubar);
this.setContentPane(content);
this.setTitle("Count Words");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack(); // Layout components.
this.setLocationRelativeTo(null); // Center window.
}

//============================================= countWordsInFile
private int countWordsInFile(File f) {

int numberOfWords = 0; // Count of words.

try {
Scanner in = new Scanner(f);

while (in.hasNext()) {
String word = in.next(); // Read a "token".
numberOfWords++;
}
in.close(); // Close Scanner's file.

} catch (FileNotFoundException fnfex) {
// ... We just got the file from the JFileChooser,
// so it's hard to believe there's problem, but...
JOptionPane.showMessageDialog(CountWords.this,
fnfex.getMessage());
}
return numberOfWords;
}

///////////////////////////////////////////////////// OpenAction
class OpenAction implements ActionListener {
public void actionPerformed(ActionEvent ae) {
//... Open a file dialog.
int retval = _fileChooser.showOpenDialog(CountWords.this);
if (retval == JFileChooser.APPROVE_OPTION) {
//... The user selected a file, get it, use it.
File file = _fileChooser.getSelectedFile();

//... Update user interface.
_fileNameTF.setText(file.getName());
_wordCountTF.setText("" + countWordsInFile(file));
}
}
}

//========================================================= main
public static void main(String[] args) {
JFrame window = new CountWords();
window.setVisible(true);
}
}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Develop an application that counts the number of words
Reference No:- TGS01249567

Now Priced at $20 (50% Discount)

Recommended (95%)

Rated (4.7/5)