Reputation: 1
I'm currently doing an assignment which requires the program to count words and punctuation from a text file. The word counting program is done and working but my professor provided an additional method to be combined with it to count punctuation that I cannot seem to get to work. Here is the working program:
import java.util.*;
import java.io.*;
public class SnippetWeek11 {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
System.out.print("Enter a filename of a text file to process: ");
String filename = input.nextLine();
File file = new File(filename);
if (file.exists()) {
processFile(file);
}
else {
System.out.println("File " + filename + " does not exist");
}
}
private static void processFile(File theFile) throws Exception {
int wordIndex;
// Create a TreeMap to hold words as key and count as value
Map<String, Integer> map = new TreeMap<>();
Scanner input = new Scanner(theFile);
String line, keyText;
String[] words;
while (input.hasNextLine()) {
line = input.nextLine();
words = line.split("[\\s+\\p{P}]");
for (wordIndex = 0; wordIndex < words.length; wordIndex++) {
keyText = words[wordIndex].toLowerCase();
updateMap(map, keyText);
}
}
// Display key and value for each entry
map.forEach((key, value) -> System.out.println(key + "\t" + value));
}
private static void updateMap(Map<String, Integer> theMap,
String theText) {
int value;
String key = theText.toLowerCase();
if (key.length() > 0) {
if (!theMap.containsKey(key)) {
// The key does not exist in the Map object (theMap), so add key and
// the value (which is a count in this case) to a new theMap element.
theMap.put(key, 1);
}
else {
// The key already exists, so obtain the value (count in this case)
// from theMap element that contains the key and update the element
// with an increased count.
value = theMap.get(key);
value++;
theMap.put(key, value);
}
}
}
And here is the method that must be combined with the word count program. I would appreciate any help you could give. Thanks.
public static int countPunctuation(File theFile) throws Exception {
String[] punctuationString = {"[","]",".",";",",",":","!","?","(",")","{","}","'"};
Set<String> punctuationSet =
new HashSet<>(Arrays.asList(punctuationString));
int count = 0;
Scanner input = new Scanner(theFile);
while (input.hasNext()) {
String character = input.next();
if (punctuationSet.contains(character))
count++;
}
return count;
}
}
Upvotes: 0
Views: 890
Reputation: 3076
If you could use Pattern
Class, you can do this.
import java.util.regex.*;
import java.util.*;
import java.util.stream.*;
class PunctuationMatch
{
public static void main(String[] args) {
final Pattern p = Pattern.compile("^[,|.|?|!|:|;]");
System.out.println(p.splitAsStream("Hello, World! How are you?").count());
}
}
While passing string in compile
method pass all the puctuation you want to identify.
Passing into splitAsStream
method your entire data string or a line by line of a file and add every thing up.
Here is the Java Docs Ref
Upvotes: 1