使用java计算单词的出现次数

时间:2017-07-26 02:53:34

标签: java csv selenium

Ticket Type Priority
    Assigned Incident 3 - Medium
    Acknowledgement Service Request 3 - Medium
    Assigned Problem 2 - High
    Assigned Incident 3 - Medium
    Assigned Service Request 3 - Medium
    Closed Incident 3 - Medium
    Assigned Service Request 3 - Medium
    Assigned Incident 2 - High
    Closed Incident 3 - Medium
    Assigned Service Request 3 - Medium
    Assigned Service Request 3 - Medium
    Acknowledgement Service Request 3 - Medium
    Assigned Service Request 3 - Medium
    Assigned Service Request 3 - Medium
    Acknowledgement Service Request 3 - Mediumc

我可以使用java selenium从网页获取上面的表格数据 我想计算

  1. 无事故
  2. 事件编号+ 3 - 中等
  3. 无事故+ 3 - 中+分配等......
  4. 并将该值存储到csv文件特定单元格中。

    我已将上表日期存储在变量中。

    public static void get_data()  {
    String txt = driver.findElement(By.xpath("//*[@class='jtrac jtrac-    list']")).getText();
    System.out.println(txt);
    }
    

    我之前使用PowerShell这样做了

    $B = $A | Select-String "Incident","Problem" | measure-object; $C = $B.Count;
    $obj[0].Measure = $C
    

    $ A包含上表数据。 Select-String选择模式字符串和度量。 $ obj逐个处理Target csv文件存储数据$ obj [*]

    但我不确定Java。

1 个答案:

答案 0 :(得分:0)

以下是代码。 Matcher对象有额外的方法可以让你获得更多。更改方法中提到的模式以满足您的需要。

 String myTotalString= "Ticket Type Priority\n" +
"    Assigned Incident 3 - Medium\n" +
"    Acknowledgement Service Request 3 - Medium\n" +
"    Assigned Problem 2 - High\n" +
"    Assigned Incident 3 - Medium\n" +
"    Assigned Service Request 3 - Medium\n" +
"    Closed Incident 3 - Medium\n" +
"    Assigned Service Request 3 - Medium\n" +
"    Assigned Incident 2 - High\n" +
"    Closed Incident 3 - Medium\n" +
"    Assigned Service Request 3 - Medium\n" +
"    Assigned Service Request 3 - Medium\n" +
"    Acknowledgement Service Request 3 - Medium\n" +
"    Assigned Service Request 3 - Medium\n" +
"    Assigned Service Request 3 - Medium\n" +
"    Acknowledgement Service Request 3 - Medium";

    private int getIncidentOrProblems()
    {
        int i = 0;
        Pattern p = Pattern.compile("Incident|Problem");
        Matcher m = p.matcher( myTotalString );
        while (m.find()) {
            i++;
        }
        return i;
    }