2017-12-01 3 views
1

ラムダ構文のJavaストリームを使用する割り当てに取り組んでいます。プログラムは設計されているはずです(1)ファイルのセットを数えるには(2)それらのファイル内の単語を数えるには(3)結果を印刷して表示します。Java 8ファイル/ワードカウントプログラムからの奇妙な出力

Count 11 files: 
word length: 1 ==> 80 
word length: 2 ==> 321 
word length: 3 ==> 643 
..... 

は、しかし、私が代わりにこの出力を取得しています: - FileCatch、ファイルとをカウント

primes.txt 
word length: 1 ==> [email protected] 
constitution.txt 
word length: 2 ==> [email protected] 
short.txt 
word length: 3 ==> [email protected] 
..... 
Count: 11 files 

私が書いたプログラムは二つのクラスであるこれは出力の例です。 WordCountは、単語を数えます(理論的には)。誰かがプログラミングのヒントを手伝ってくれたら、私は感謝します。

FileCatchクラス

public class FileCatch8 { 
    public static void main(String args[]) { 
     List<String> fileNames = new ArrayList<>(); 
     try { 
      DirectoryStream<Path> directoryStream = Files.newDirectoryStream 
     (Paths.get("files")); 
      int fileCounter = 0; 
      for (Path path : directoryStream) { 
       System.out.println(path.getFileName()); 
       fileCounter++; 
       fileNames.add(path.getFileName().toString()); 
       WordCount WordCnt = new WordCount(); 
       System.out.println("word length: " + fileCounter + " ==> " + WordCnt); 
      } 
    }catch(IOException ex){ 
    } 
    System.out.println("Count: "+fileNames.size()+ " files"); 

    } 
} 

WordCountクラス:

import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.util.AbstractMap.SimpleEntry; 
import java.util.Arrays; 
import java.util.Map; 
import java.util.TreeMap; 
import static java.util.stream.Collectors.counting; 
import static java.util.stream.Collectors.groupingBy; 
import java.util.stream.Stream; 

/** 
* 
* @author GeraldShields 
*/ 
public class WordCount { 

    /** 
    * 
    * @return 
    * @throws IOException 
    */ 
    public Map<String, Long> WordCount()throws IOException { 
     Stream<String> lines = Files.lines(Paths.get("constitution.txt")); 
     Map<String, Long> wordMap = lines 
       .parallel() 
       .map(String::toLowerCase) 
       .map(line -> line.split("\\W+")) 
       .flatMap(line -> Arrays.asList(line).stream()) 
       .filter(word -> !word.matches("\\d+") && word.trim().length() != 0) 
       .map(word -> new SimpleEntry<>(word, 1)) 
       .collect(groupingBy(SimpleEntry::getKey, counting())); 
     new TreeMap(wordMap).forEach((k, v) -> 
       System.out.println(String.format("%s word length: 1 ==> %d", k, v))); 
     return wordMap; 
    } 
} 
+0

私はそれを使用してファイルに番号を付けています。例:ファイル1、ファイル2など –

+0

@Eran誰かが配列を見つけて、前に完了したと思ったからです! –

+0

@Eran intカウンタが機能しました。ありがとうございました。しかし、私はもう質問を編集するオプションがありません。私はintを置いた後に[email protected]ものを持っていますfileCounter = 0; forループのfileCounter ++とprintln文のfileCounterのプラグイン。 –

答えて

0

あなたは意味をなさないあなたのWordCountクラスのインスタンスを印刷しています。また、WordCount()メソッドを呼び出すこともありません。

あなたはWordCountの単一のインスタンスを作成し、単語が(私は、コードを読みやすくするためのメソッドや変数のいくつかを改称)ファイルごとにカウント実行するメソッドを呼び出す必要があります:

WordCount wordCnt = new WordCount(); 
for (Path path : directoryStream) { 
    System.out.println(path.getFileName()); 
    fileCounter++; 
    fileNames.add(path.getFileName().toString()); 
    System.out.println("word length: " + fileCounter + " ==> " + wordCnt.count(path.getFileName().toString())); 
} 


public class WordCount { 

    /** 
    * 
    * @return 
    * @throws IOException 
    */ 
    public Map<String, Long> count(String filename) throws IOException { 
     Stream<String> lines = Files.lines(Paths.get(filename)); 
     Map<String, Long> wordMap = lines 
       .parallel() 
       .map(String::toLowerCase) 
       .map(line -> line.split("\\W+")) 
       .flatMap(line -> Arrays.asList(line).stream()) 
       .filter(word -> !word.matches("\\d+") && word.trim().length() != 0) 
       .map(word -> new SimpleEntry<>(word, 1)) 
       .collect(groupingBy(SimpleEntry::getKey, counting())); 
     new TreeMap(wordMap).forEach((k, v) -> 
       System.out.println(String.format("%s word length: 1 ==> %d", k, v))); 
     return wordMap; 
    } 
} 

注意WordCountクラスは、ファイル内の単語の総数ではなく、ファイル内の各単語の出現数をカウントしているようですので、質問の開始時に投稿した予想出力と一致していないようです。

+0

@Erenチェックするだけです:WordCountメソッドを作成することをお勧めしましたか?私はこれらの提案を試みたが、コンパイラはSystem.out.println( "word length:" + fileCounter + "==>" + wordCnt.count(path.getFileName()。toString()))に問題があった。 –

+0

@JavaNewbieいいえ、あなたの 'WordCount'メソッドの名前を' count'に変更し、 'String'引数を追加しました。 – Eran

+0

@JavaNewbie 'wordCnt.count(path.getFileName()。toString())'がコンパイルをパスするためには、 'WordCount'クラスを変更する必要があります。 – Eran