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

  Write a payroll applicaion in java

The program should allow the user to enter information for any number of employees in this manner.

  Display an image of an apple on the screen

Display an image of an apple on the screen, then after 3 seconds automatically change the display to an orange.

  Create a java program to calculate

Create a Java Program to calculate the all grades inputted by a student, the average grade for the student, the letter grade for the student, and the valid inputted scores displayed in ascending order.

  Create a messageframe class extending jframe

Create a MessageFrame class extending JFrame and a MessagePanel class extending JPanel.

  Create the java code for a generic class

Create the Java code for a generic class. The class should contain at least one private data member of a generic type, a constructor which initializes the private data member to the value of a passed in argument, and a getter and setter for the ge..

  Development of database systems

Prior to the development of database systems, file I/O was how information was stored and retrieved. Give your opinion on what you believe we have gained by using databases. Explain whether or not you believe files had any advantages.

  Prepare a java simulation program

Given a hash function h, prepare a java simulation program to determine each of the subsequent quantities after 0.8*tablesize random keys have been generated. The keys should be random integers.

  Java statement to initialize a variable square

Write a Java statement to initialize a variable square with a rectangle object whose top left corner is (10, 20) and whose sides all have length 40. Then write a statement that replaces square with a rectangle of the same size and top left corner ..

  Write a program that enables elevator-nxtrobot to be control

Write a program that enables theElevator-NXTrobot to be controlled manually using the NXT's front panel buttons (LEFT for door open, RIGHT for door close, ENTER for up, and ESCAPE for down), as demonstrated in thisvideo.

  Design and code a swing gui to translate test

Use a FLOWLAYOUT with a JTEXTAREA for the source text and separate JTEXTAREA for the translated text. Add a JBUTTON with an event to perform the translation

  Learn how expressions can be evaluated at run-time

Write a Java program (a collection of Java classes) including a class named ExpressionCalculator that contains a static main method. This main method will prompt the user to enter a constant expression.

  What was in mind of the coder in each part of the code

Need a video that goes through all this java code - as if it was a tutorial showing it line by line - and explain in detail the structure of it all as if you code it yourself.

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