Java Tutorials

How To Count Words and Characters In a Text In Java

“How To Count Words and Characters In a Text In Java” is a program that takes a file (typically .txt) and shows how many words and characters there are in the given file. It also provide a grade for the text which is based on the mean of words length. If the result of dividing number of characters by number of words gets greater than 6 the grade is “First” otherwise is “Second Upper”.

There are one Java class and one file for the program :

The Marking Class :



import java.io.BufferedReader;
import java.io.FileReader;
public class Marking {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
int max = 150;
String lines[] = new String[max];
FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fr);
String d;
int i=0;
int numberOfWordsLine = 0;
int numberOfWords = 0;
int numberOfChars = 0;

while((d=br.readLine())!=null)
{
String[] regexStr;
int counter = 0;
lines[i]=d;
numberOfWordsLine = lines[i].split(" ").length;
regexStr = new String[numberOfWordsLine];
regexStr = lines[i].split(" ");
for(int j = 0; j < numberOfWordsLine; j++) {
counter += regexStr[j].length();

}
numberOfWords+= numberOfWordsLine;
numberOfChars +=counter;
i++;
}
System.out.println("********");
System.out.println("Number Of words in text = " + numberOfWords);
System.out.println("Number Of characters in text = " + numberOfChars);

int mean = numberOfChars / numberOfWords;
if(mean > 6) {
System.out.println("Your Grade is First");
} else {
System.out.println("Your Grade is Second upper");
}

}

}

The file input.txt

The purpose of this assignment is to provide us with an opportunity for learning in which the aim is to develop and build
a functional database system that encourages experimentation and learning.
Our goal is to create this system for a bike hire company called ‘Ray’s Rentals’ allowing the business to benefit
from the advantages an automated system can give, as well as finding solutions to current problems
within the old system and express concerns to find a solution to these problems with our new system.
Most importantly this assignment will show how our group can manage time and split up tasks between us accordingly to get work done
in an efficient way as well as giving us plenty of time to carefully asses all criteria’s of this project’s brief.
Similarly it will show how each individual group member can perform separate tasks assigned to them and not hold the rest of the group back.
We will be thoroughly covering all aspects of the new System; going through the design stage creating the tables needed
and formalising them so that they can be implemented in a professional manner as well as drawing up group ERD and UCD diagrams.
We will also compare the advantages of a computerised system over the current paper based one and
ultimately how this system will be a great benefit to Ray’s Rentals. We will then create the database providing all the working functions
needed to create the system as an SQL database.
Finally we will personally share our thoughts and explain what we have learned and how this project has enlightened
us on the importance of Database Management.

Leave a Reply

Your email address will not be published. Required fields are marked *