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