0
我目前正在做一項任務,要求程序從文本文件中計算單詞和標點符號。單詞計數程序已經完成並且正在工作,但是我的教授提供了一種額外的方法來結合它來計算我似乎無法工作的標點符號。這裏是工作程序:檢查並計算文件中的標點符號
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);
}
}
}
這裏是必須與字計數程序結合的方法。我將不勝感激任何幫助。謝謝。
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;
}
}
你比較'對單個字符'Strings',如果你的標點符號是從用空格隔開的話這將工作Strings',但我收集他們不是(例如'」結束。「'將需要''結束。」')。 'input.next()'解析出由一個或多個空格字符('\ s','\ t',...)分隔的'String'。如果你想遍歷每個字符,那麼你應該使用'input.useDelimiter(「。」)''將'input'的分隔符更改爲'.'。 –