我目前正在进行一项任务,要求程序从文本文件中计算单词和标点符号。单词计数程序已经完成并正在运行,但是我的教授提供了一个额外的方法与它结合起来计算我似乎无法开始工作的标点符号。这是工作计划:
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;
}
}
答案 0 :(得分:1)
如果您可以使用Pattern
课程,则可以执行此操作。
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());
}
}
在compile
方法中传递字符串时,会传递您要识别的所有标识符。
将splitAsStream
方法传入整个数据字符串或逐行传入,并添加所有内容。