2016-11-20 7 views
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; 
    } 
    } 
+0

です終わり "は"終わり "である必要があります)。 'input.next()'は、1つ以上のスペース文字( '\ s'、' \ t'、...)で区切られた 'String'を解析します。すべての文字をステップインしたい場合は、 'input.useDelimiter("。 ")'を使用して 'input'の区切り文字を' .'に変更する必要があります。 –

答えて

1

Patternクラスを使用できる場合は、これを行うことができます。 compile方法で文字列を通過させながら

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()); 
    } 
} 

はあなたが識別したいすべてのpuctuationを渡します。

あなたのデータ文字列全体またはファイルの行ごとにメソッドを渡して、すべてのものを追加します。ここで

はあなたの句読点が空白を含む単語から分離された場合に働くだろう一文字 `Strings`に対して` Strings`を、比較しているが、私は、彼らが(たとえば `」じゃない集まるJava Docs Ref

関連する問題