Hi all,
I'm a student learning Java right now, so I'm still very new to what Java can do.
I've written a program to perform a word count (thanks to VernonDozier for help with case-sensitivity) but another problem has come up. The program is identifying words with punctuation to be different from words without punctuation - for example, "process" would be considered different from "process.", as if it were at the end of a sentence.
The reason this is important is that it's part of the assignment requirements for the program to be able to identify distinct words.
I currently have no idea how to address this whatsoever. All help is appreciated!
Thanks!
The program is as follows:
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) {
a = a.toLowerCase();
System.out.println(a);
Integer freq = textInput.get(a);
textInput.put(a, (freq == null) ? 1 : freq + 1);
}
// 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());
}
}
}