2017-04-23 147 views
1

此代码的目的是对使用HashMaps和Streams的顺序出现的字母的实例进行计数。从arrayList中删除双括号到单个括号

代码打印出

<[[is=3,imple=2,it=1]]> 

,但我需要它通过这个测试

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

我使用的ToString()来解决这个问题,但在取出一套支架尝试使用.replace()导致预期结果更改为

<is=3[ 
imple=2 
it=1 
]> 

和我的实际代码打印

<is=3[,imple=2,it=1]> 

我试着在toString()中使用.replace删除逗号,但是我觉得我在那个时候硬编码了答案。

任何关于如何进行的建议?非常感谢!

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.Arrays; 
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>>(); 

    results.addAll(counts.entrySet()); 
    Collections.sort(results, Collections.reverseOrder(Map.Entry.comparingByValue())); 

    String aList = results.toString(); 

    aList = results.toString().replace("[", "").replace("]", ""); 
    printstream.print(aList); 
} 





} 



//Test Case 

enter code here 
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

是否有一个特定的原因,你有你的断言“_”?你不能用逗号检查。为什么行分隔符? – Sasang

+0

仅仅是因为我要将这段代码应用到脚本的.txt文件中,以便计算单个单词的出现次数,并希望它们在各个单词之间用新行进行分析以便于识别。 – Jake

+0

没错,但是你在替换“_”后断言。为了便于阅读,您可以在测试后对其进行格式化。 – Sasang

回答

0

以下行在test_WordCount_report()方法执行之后:串出= output.toString()。输出应该是这样的:

is=3, imple=2, it=1 

所以,你应该能够直接断言,或者如果你需要保持这一特定的断言字符串,改变断言检查,像这样:

String assertValue = "is=3_imple=2_it=1_"; 
String updAssert = assertValue.substring(0,assertValue.length() - 1).replace("_", ", "); 
assertEquals(updAssert, out); 

后来在代码中,您可以将updAssert(如果它传递)附加到字符串生成器,然后附加一个新行。

StringBuilder sb = new StringBuilder(); 
sb.append(updAssert).append(System.lineSeparator()); 
+0

为了解决我的问题,我最终做的是删除.toString()替换.toArray()并使用for循环迭代printstream.print 非常感谢您的帮助! – Jake