2017-04-23 47 views
0

此代碼的目的是計算使用HashMaps和Streams順序出現的字母的實例。計算字母序列的實例

我遇到了我的System.out.print(結果)的問題是打印[is = 3,imple = 2,it = 1]到控制檯,但我的junit說「預計< [是= 3 Imple = 2 it = 1]>但是< []>。

該代碼打印出[is = 3,imple = 2,it = 1],但它似乎並未實際更新到內存中。對我應該做的任何提示或建議? 太謝謝你了!

import java.util.HashMap; 
import java.util.LinkedList; 
import java.util.Map; 
import java.util.Map.Entry; 
import java.util.Scanner; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
import java.io.ByteArrayOutputStream; 
import java.io.PrintStream; 
import java.util.List; 
import java.util.ArrayList; 
import java.util.Collections; 

public class WordCount { 

protected Map<String, Integer> counts; 
static Scanner in= new Scanner(System.in); 

public WordCount(){ 

    counts = new HashMap<String,Integer>(); 

} 
public Map getCounts(){ 

    return counts; 
} 

public int parse(Scanner in, Pattern pattern){ 
     int counter=0; 
     while (in.hasNext()) { 
     // get the next token 
     String token = in.next(); 
     // match the pattern within the token 
     Matcher matcher = pattern.matcher(token); 
     // process each match found in token (could be more than one) 
     while (matcher.find()) { 
         // get the String that matched the pattern 
      String s = matcher.group().trim(); 
         // now do something with s 

      counter=counts.containsKey(s) ? counts.get(s):0; 
      counts.put(s,counter+1); 
      } 

     } 


     return counter; 
} 


public void report(PrintStream printstream){ 
    List<Map.Entry<String, Integer>> results = new ArrayList<Map.Entry<String, Integer>>(); 
    for(Map.Entry<String, Integer> entry: counts.entrySet()){ 
     results.add(entry); 
     Collections.sort(results,Collections.reverseOrder(Map.Entry.comparingByValue())); 
     results.toString(); 

    } 



    System.out.println(results); // The main problem is this outputs [is=3, 
imple=2, it=1] but the junit doesn't pass. 


} 


} 

//Test Cases 


import java.io.ByteArrayOutputStream; 
import java.io.PrintStream; 
import java.util.Scanner; 
import java.util.regex.Pattern; 

import junit.framework.TestCase; 

public class TestWordCount extends TestCase { 
public void test_WordCount_parse() { 
    WordCount wc = new WordCount(); 
    Scanner in = new Scanner("this is a simple test, but it is not simple to pass"); 
    Pattern pattern = Pattern.compile("[i][a-z]+"); 
    wc.parse(in, pattern); 

    assertEquals((Integer)3, wc.getCounts().get("is")); 
    assertEquals((Integer)2, wc.getCounts().get("imple")); 
    assertEquals((Integer)1, wc.getCounts().get("it")); 

} 

    public void test_WordCount_report() { 
    WordCount wc = new WordCount(); 
    Scanner in = new Scanner("this is a simple test, but it is not simple to pass"); 
    Pattern pattern = Pattern.compile("[i][a-z]+"); 
    wc.parse(in, pattern); 

    ByteArrayOutputStream output = new ByteArrayOutputStream(); 
    wc.report(new PrintStream(output)); 
    String out = output.toString(); 
    String ls = System.lineSeparator(); 

    assertEquals("is=3_imple=2_it=1_".replace("_", ls), out); 
    } 
+0

使您的報告方法顯式返回結果。 –

+0

因此,將「System.out.println(results)」更改爲「return(results)」似乎會導致相同的輸出 – Jake

回答

0
`public void report(PrintStream printstream)` 

在這種方法中,你不PRI nt到printstream。嘗試加入

printstream.print(results); 

到這個方法。

請注意,雖然System.out本身是PrintStream,但它是與控制檯綁定的不同流。

+0

謝謝,這使我得到了實際的輸出結果!我現在有輸出<[[is = 3 ,imple = 2,it = 1]]>但這可能是由於我的報告方法中有一些基本的東西,非常感謝您的幫助! – Jake