在同一行上出现字符串

时间:2013-03-06 20:01:26

标签: java hashmap bufferedreader indexof

我有一些代码可以计算文本文件中一周中几天的出现次数。截至目前,如果它是该行上唯一的字符串,它将只计算星期几。例如,如果我有一行说(星期一abcd),它将不计算该星期一的计数。我尝试使用indexOf并通过拆分,修剪和添加回hashmap来解决这个问题,但我无法弄清楚如何做到。

下面是一些代码,在此之前我声明关键字,打开文本文件并将每个关键字放在地图中,其值为零

public class DayCounter
{

public static void main(String args[]) throws IOException 
{

    String[] theKeywords = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

    // put each keyword in the map with value 0 
    Map<String, Integer> DayCount = new HashMap<String, Integer>();
    for (String str : theKeywords)
    {
        DayCount.put(str, 0);
    }

    try (BufferedReader br = new BufferedReader(new FileReader("C:\\Eclipse\\test.txt")))
    {

String sCurrentLine;

// read lines until reaching the end of the file
 while ((sCurrentLine = br.readLine()) != null) 
 {


   if (sCurrentLine.length() != 0) 
    {

    // extract the words from the current line in the file
     if (DayCount.containsKey(sCurrentLine))
     {
      DayCount.put(sCurrentLine, DayCount.get(sCurrentLine) + 1);
     }
    }
  }

并且继承了输出部分

 for(String day : theKeywords)
 {
  System.out.println(day + " = " + DayCount.get(day));

 }

2 个答案:

答案 0 :(得分:1)

您需要在字符串中搜索一周中的实际日期。现在你问“DayCount是否包含一个[整行]名称的密钥”,你想要的是检查每一行中每一天的所有事件。快速而肮脏的方法是将字符串拆分为该字(例如“Monday”)并计算结果列表的长度:

while ((sCurrentLine = br.readLine()) != null) {
    // For every line in the reader...

    for (String dayOfWeek : (Set<String>) DayCount.keySet()) {
        // For each day of the week (the keys in the DayCount map), count how
        // many times that key shows up in the line.
        int occurrences = sCurrentLine.split(dayOfWeek, -1).length - 1;

        // Now increase the appropriate counter by the number of occurrences (0+)
        DayCount.put(dayOfWeek, (Integer) DayCount.get(dayOfWeek) + occurrences);
    }
}

由于你在迭代Set时遇到问题(这是一个谜,但不在原问题的范围内),你也可以像这样写(就像我在评论中提到的那样 - 注意改变内环):

while ((sCurrentLine = br.readLine()) != null) {
    // For every line in the reader...

    //NOTE: I strongly advise renaming theKeywords to something more descriptive!
    for (String dayOfWeek : theKeywords) {
        // For each day of the week, count how many times that key shows up.
        int occurrences = sCurrentLine.split(dayOfWeek, -1).length - 1;

        // Now increase the appropriate counter by the number of occurrences (0+)
        DayCount.put(dayOfWeek, (Integer) DayCount.get(dayOfWeek) + occurrences);
    }
}

这一切都很简单;唯一奇怪的是:

int occurrences = sCurrentLine.split(dayOfWeek, -1).length - 1;

此代码在当前行上调用split方法。它在一周的一天中分开,并且作为“maxSplits”看起来很笨拙-1。此负值告诉split方法在结果中的行尾包含空字符串。否则,虽然行"a b c Monday "将按预期返回长度为2的数组(["a b c ", " "]),但行"a b c Monday"(末尾没有空格)将返回长度为1的数组,因为最后一项是空的。

一旦我们在一周的星期几中得到正确的数组,我们就会计算其中的项目数并减去一个以获得实际的出现次数。这总是合法的,因为我们的数组将具有的最小大小为1(在没有发生拆分的情况下,因此原始String是返回数组中的唯一元素)。

答案 1 :(得分:0)

DayCount类的规格是什么?很难在不知道的情况下弄清楚代码的意图是什么。

无论如何,你可以使用contains方法判断一行是否包含星期几;

if(sCurrentLine.contains("Monday") || sCurrentLine.contains("Tuesday") || ...) then ...