Overview reference



Generate a program that counts the number of words in a given text. For added complexity, read a file and count the number of words in the content.


  • If the user provides a file path as the first command line argument, read the file and count the words
  • Else, ask the user for input and count the words


Count the words

My definition of a word here is a group of characters separated by one or multiple spaces. As a result a full stop may be present in a “word” along with several other non-alphanumeric characters.

private static int countwords(String text ) {
		int count = 0;
		String[] splitted = text.split("\\s+");
		return splitted.length;

Reading a file

First of all, finding this solution was simple; clicked on the first stack overflow search result and bam. This method simply returns the entire content of the file in a variable. This may be questionable for extremely large files, however we will not worry about that now. Try/catch blocks were added to “catch” errors related to the file reading step. An exception will be thrown when the given file is not found or does not exist.

private static String readfile(String filepath) {
		String lines = "";
			String content = new String(Files.readAllBytes(Paths.get(filepath)));
		    // do something with everything string
		    lines += content;
		}catch (IOException e) {
			// TODO Auto-generated catch block
		return lines;

Trigger Action

public static void main(String[] args) {
		// TODO Auto-generated method stub
		if(args.length > 0) {
			String filepath = args[0];
			String content = CountWordsinaString.readfile(filepath);
			int count = CountWordsinaString.countwords(content);
			System.out.println("The number of words is: " + count);
		}else {
			System.out.println("Type in text to count and press enter");
			Scanner sc = new Scanner(System.in);
			String line = sc.nextLine();
			int count = CountWordsinaString.countwords(line);
			System.out.println("The number of words is: " + count);

Run Program

File Path as a command line argument

C:<path>\com.learnjava\src\com\learnjava\beginner>java CountWordsinaString.java "lang list.txt"
The number of words is: 1648

Text provided by user

C:<path>\com.learnjava\src\com\learnjava\beginner>java CountWordsinaString.java
Type in text to count and press enter
My friend is doing well thanks
The number of words is: 6


  • How would you generate a word frequency report of the file you read ?
  • How would you count the words in each file from a list of files given by the user as command line arguments.


%d bloggers like this: