Here is my code for word count from command line. Ive got it reading what is in the command line but its not reading the file it just reading the words from the command line.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class WordCount {
	public static void main(String[] args) {
		// Defining total word count variable
		int totalCount = 0;
		// Setting up linked hash map so output will display words in order of
		// appearance
		Map<String, Integer> textInput = new LinkedHashMap<String, Integer>();
		// Determining the number of distinct words and their frequencies of
		// occurrence
		for (String a : args) {
			String myFileName = a;
			Integer freq = textInput.get(a);
			textInput.put(a, (freq == null) ? 1 : freq + 1);
			try {
				// Make a file for scanner to read from
				File fileToRead = new File(myFileName);

				// Make a scanner from the file
				Scanner fileScanner = new Scanner(fileToRead);

				/*
				 * While file has content, gets each new string up to next
				 * whitespace. I only print them, you can process them however.
				 * !!Your desired data types and processing sequence will vary!!
				 */
				
				while (fileScanner.hasNextLine()) {
					// Get next string & print it
					}
				// Closing the scanner when done is good practice
				fileScanner.close();
			}// end try block
			catch (FileNotFoundException e) {
				System.out.println("Couldn't find: " + myFileName);
				e.printStackTrace();
			}// end catch block
			// Determining the word count using the occurrence frequencies
			for (int Values : textInput.values())
				if (Values >= 2) {
					totalCount += Values;
				} else {
					++totalCount;
				}
			// Determining correct grammar and printing word count results
			// If there's only one word
			if (totalCount == 1) {

				System.out.println("The total word count is " + totalCount
						+ " word.");
				//System.out.println("The word is " + textInput.keySet());
			}
			// If there's no words
			else if (totalCount == 0) {

				System.out.println("There are no words.");

				// If there's more than one word
			} else {
				System.out.println("The total word count is " + totalCount
						+ " words.");
				//System.out.println("There are " + textInput.size()
				//		+ " different words.");
				//System.out.println("The words are: " + textInput.keySet());
			}
		}
	}
}

Hi,
In the code I cannot see the declaration of the file you are using. Are you inputting the file name at the start of the program?

I am inputting it from the command line.

Hi,
In the code I cannot see the declaration of the file you are using. Are you inputting the file name at the start of the program?

Hi Again,
I modified the code you posted because when I was going through your code, i found out that the program does not go to reading the file part even if you input the file name. So i made a file named as WordCount.txt and wrote:

hi
hello
how are you?

I use the following code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication2;

/**
 *
 * @author jahan
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class Main {
	public static void main(String[] args) {
		// Defining total word count variable
		int totalCount = 0;
		// Setting up linked hash map so output will display words in order of
		// appearance
		Map<String, Integer> textInput = new LinkedHashMap<String, Integer>();
		// Determining the number of distinct words and their frequencies of
		// occurrence
                

			String myFileName = "WordCount.txt";
		//	Integer freq = textInput.get(myFileName);
		//	textInput.put(a, (freq == null) ? 1 : freq + 1);
			try {
			        
                            // Make a file for scanner to read from
				File fileToRead = new File(myFileName);

				// Make a scanner from the file
				Scanner fileScanner = new Scanner(fileToRead);

				/*
				 * While file has content, gets each new string up to next
				 * whitespace. I only print them, you can process them however.
				 * !!Your desired data types and processing sequence will vary!!
				 */

                                String s=fileScanner.nextLine();
				while (s!=null) {

                                        System.out.print(s+"\n");
                                        s=fileScanner.nextLine();
					// Get next string & print it
					}
				// Closing the scanner when done is good practice
				fileScanner.close();
			}// end try block
			catch (FileNotFoundException e) {
				System.out.println("Couldn't find: " + myFileName);
				e.printStackTrace();
			}// end catch block
			// Determining the word count using the occurrence frequencies
			for (int Values : textInput.values())
				if (Values >= 2) {
					totalCount += Values;
				} else {
					++totalCount;
				}
			// Determining correct grammar and printing word count results
			// If there's only one word
			if (totalCount == 1) {

				System.out.println("The total word count is " + totalCount
						+ " word.");
				//System.out.println("The word is " + textInput.keySet());
			}
			// If there's no words
			else if (totalCount == 0) {

				System.out.println("There are no words.");

				// If there's more than one word
			} else {
				System.out.println("The total word count is " + totalCount
						+ " words.");
				//System.out.println("There are " + textInput.size()
				//		+ " different words.");
				//System.out.println("The words are: " + textInput.keySet());
			}
		}
	}

Output:
hi
hello
how are you?
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1533)
at javaapplication2.Main.main(Main.java:49)
Java Result: 1

Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.