Develop an application that counts the number of words

Assignment Help JAVA Programming
Reference no: EM13940255

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);
}
}

Reference no: EM13940255

Questions Cloud

Cost formula for its supplies cost : Ingram Framing's cost formula for its supplies cost is $1,120 per month plus $12 per frame. For the month of June, the company planned for activity of 610 frames,
What is role of material requisition forms in costing system : What is the role of materials requisition forms in a job-order costing system? Time tickets? Predetermined overhead rates?
Components of the new product development process : Choose two components of the new product development process e.g. idea generation, product development, market analysis, commercialization or product launch and describe and analyse the product development strategy adopted by an energy company as ..
Document supersedes all oral promises relating to the sale : In a transaction for the sale of an auto painting facility, Bright Auto Colors Company tells Custom Cars Corporation that the paints and other supplies on-site are included. The contract says nothing about the supplies on-site, but does state, “This ..
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
Production facilities across the country : A privately-owned manufacturing firm has three production facilities across the country. Here are some important facts about these facilities:
Proposed above of reducing selling price and undertaking : Using an excel sheet that shows the relevant formulas; calculate the break-even point (in units as well as in dollars)for the original budgeted income statement provided above.
What recommendations would you make to correct it : How can managers determine if organizational structure is out of alignment with an organization's strategy in a criminal justice organization? If the organizational structure were misaligned, what recommendations would you make to correct it?
Discuss the organisation of a maintenance department : 1. Discuss the organisation of a maintenance department of a large hotel. State the factors for which the maintenance staff is responsible. (1/2 page)

Reviews

Write a Review

JAVA Programming Questions & Answers

  Discuss the use of a binary tree

Discuss the use of a binary tree when searching for keys in an array. Discuss the use of a binary tree when searching for keys in a linked list

  Write a program to track hourly employee arrival departure

THE JAVA SOURCE CODEWILL AWARD AN ADDITIONAL 1,500 POINTS TO PERSON WITH THE BEST ANSWER (you will be directed to another one of my previous, unanswered questions.)A company hires you to write a program to track hourly employee arrival and departure ..

  Socket programming in java: tcp

In this project we will develop a Web server in two steps. In the end, you will have built a multi-threaded Web server that is capable of processing multiple simultaneous service requests in parallel.

  Determine values of the side of triangle

Enter 3 integers and determine if these 3 values make up the sides of a triangle. If any side is

  Write the string copy and string concatenation functions

Write the string copy and string concatenation functions and first version should use array subscripting, and the second version should use pointer arithmetic.

  Prepare a class called geometrycalculator

Calculates the area of a circle of a given radius and write a class called GeometryCalculator that includes the subsequent methods

  Achieve these two important features

Java TM is a portable language, and being an object-oriented programming language, it also encourages component reusability. How does Java TM achieve these two important features

  Program that allows users to enter a dollar amount

Write a program that allows users to enter a dollar amount for their bank account balance at the beginning of the month. Then ask the user to enter a dollar amount for any number of checks written in a month, up to 50.

  Prepare demo program that implements all of above method

Specify, design, and implement a class that can be used in a program that simulates a combination lock.

  Determine the example of a javascriptr framework in the

what is an example of a javascriptr framework? in the framework you have described what is an example of an application

  Create and code a program that uses three parallel numeric

Create and code a program that uses three parallel numeric arrays of size 6. The program searches one of the arrays and then displays the corresponding values from the other two arrays.

  System schedule on a multiprocessor-multicore environment

This program assignment is provided to let the students understand how to the Operating Systems schedule all the processes on a multiprocessor or multicore environment.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd