对于带有字数的Java中的每个循环

时间:2015-09-08 07:00:20

标签: java arrays string for-loop foreach

我有一个长文本存储在String(即tstr1 im代码)中。现在我想将来自控制台的用户输入存储在String[](即itemsFromArray im代码)中。

我想要存储在用户输入String []数组中的每个单词,系统显示该单词在长文本字符串[]数组中出现的次数。我尝试过这种方式,但问题是系统只显示数组中的第一个条目,但下一个显示为零。

  btnNewButton.addActionListener( new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            keyW = txtKeyword.getText();
            search = textField.getText();
            System.out.println("String for car = " + search); 
            System.out.println("String keyword = " + keyW); 

        WebDriver driver = new FirefoxDriver();
        driver.get("https://en.wikipedia.org/wiki/" + search);
        tstr1 = driver.findElement(By.xpath("//*[@id='content']")).getText();

        String [] itemsFromArray = keyW.split(",");  
        Map<String, Integer> map = new HashMap<String, Integer>();
        for (String word : itemsFromArray){
            map.put(word, 0);
        }
         Scanner s = new Scanner(tstr1);
        while (s.hasNext()){
            String word = s.next();
            if (map.containsKey(word)){
                map.put(word, map.get(word) + 1);
                System.out.println("Word1 '" + word + "' count:" + map.get(word));
            } else {
                System.out.println("Word2 '" + word + "' not in map");
            }
        }

        driver.close();  
        }   
    }); 

2 个答案:

答案 0 :(得分:2)

最好使用Map<String, Integer>

// initialize a mapping that is used to map words to their count   
Map<String, Integer> counter = new HashMap<String, Integer>();

// initialize all counts to 0
for (String word : itemsFromArray){
    counter.put(word, 0);
}

// ...

// count words that are in map (give up those that aren't)
while (s.hasNext()){
    String word = s.next();
    if (counter.containsKey(word)){
        counter.put(word, counter.get(word) + 1);
        System.output.println("Word '" + word + "' count:" + counter.get(word));
    } else {
        System.output.println("Word '" + word + "' not in map");
    }
}

答案 1 :(得分:1)

在我花了一天之后,问题是当我尝试将字符串(没有逗号)和插入数组分开时,第一个字符串很好,但是下一个字符串以空格开头,系统无法识别该字。

    btnNewButton.addActionListener( new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            //If the button UPLOAD was not pressed we should to clear the ArrayList
            listKeys.clear();
            //////////////////////////////////////////////

            if(textField.getText().equals("")) {

                JOptionPane.showMessageDialog(null,"Make sure you enter at least one search key"); 

            }

       else if (txtKeyword.getText().equals("")) {

           System.out.println("String is NULL ");

           JOptionPane.showMessageDialog(null,"Add at least one keyword"); 
       } else {

            keyW = txtKeyword.getText();
            search = textField.getText();
            System.out.println("String for car = " + search); 
            System.out.println("String keyword = " + keyW); 

        WebDriver driver = new FirefoxDriver();
        driver.get("https://en.wikipedia.org/wiki/" + search);
        tstr1 = driver.findElement(By.xpath("//*[@id='content']")).getText();

        String [] items = keyW.split(","); 
        String [] itemsFromArray = new String[items.length];
        for ( int i = 0; i <  items.length; i++)
        {
            itemsFromArray[i] = items[i].trim();
        }
            for(String string : itemsFromArray)
        {       
            //if (args[i].toLowerCase().startsWith(­ "from:" )) 

            System.out.println("FOREACH " + string);
            int i = countWords(tstr1, string);
            System.out.println("Word count "+ string + ":  " + i);
            Keyword1 = ("Count for word " + string + " are " + i);
            listKeys.add(Keyword1);
        }
    driver.close();   
    }   

        }




private static int countWords(String tstr1, String string)
    {   
        int i = 0;
        Scanner s = new Scanner(tstr1);

        while (s.hasNext()) 
        {
            if (s.next().equals(string)) 
                i++;

        }

        return i;
    }
}