java 检查和计算文件中的标点符号

qlfbtfca  于 2023-06-28  发布在  Java
关注(0)|答案(1)|浏览(80)

我目前正在做一个作业,要求程序从一个文本文件中计算单词和标点符号。单词计数程序已经完成并开始工作了,但是我的教授提供了一种额外的方法来与它结合起来计数标点符号,我似乎不能开始工作。以下是工作程序:

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;
    }
   }
e0bqpujr

e0bqpujr1#

如果你可以使用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方法传入整个数据串或文件的一行一行,然后将所有内容相加。
这里是Java Docs Ref

相关问题