1

Hi I am trying to get the word count in a paragraph. The following is my code to find the count. Kindly check the code and tell me the mistake.

Code
----
package checking;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;

public class WordCount {

    public static void main(String[] args) {
        RemoteWebDriver driver;

        //driver = new FirefoxDriver();

        System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
        driver = new ChromeDriver();

        driver.manage().window().maximize();
        driver.get("https://jqueryui.com/");

        WebElement Para = driver.findElement(By.xpath("//*[@id='banner-secondary']/p"));
        String ParaCon = Para.getText();

        System.out.println("Paragraph Content : "+ParaCon);

        int ParaConCount = ParaCon.length();
        System.out.println("Paragraph Characters Count : "+ParaConCount);

        int count=0;
        for (int i = 0; i <= ParaConCount; i++) {
            if (ParaCon.contains("of")) {
                count++;
                break;
            }
        }
        System.out.println("Count of Word present : "+count);

        driver.quit();
    }

}

It should get thecount of thw word whatiam giving

Vijay
  • 189
  • 1
  • 2
  • 14
  • Actually you break the moment you find it once, which means you can only find it once (count max = 1). Check out this question to see how you can get the amount of times a substring appears in a string: http://stackoverflow.com/questions/767759/occurrences-of-substring-in-a-string – Floris Velleman Apr 26 '16 at 10:00
  • Ok i will check. Thanks Floris Velleman – Vijay Apr 26 '16 at 10:04
  • try this it will help http://stackoverflow.com/questions/5864159/count-words-in-a-string-method – Rajnish Kumar Apr 26 '16 at 10:04
  • I tried the code u gave but i am facing problem. = – Vijay Apr 26 '16 at 10:13

2 Answers2

0

Using an example:

ParaCon = "of of of"

This will give you:

ParaConCount = 8

And a for loop that looks like this:

int count = 0;
for (int i = 0; i <= 8; i++) {
     if ("of of of".contains("of")) {
         count++; // count is now 1
         break; // We get out of the loop here (so we have count = 1)
     }
}
// count is now 1

What about using:

StringUtils.countMatches(ParaCon, "of"); // this will return 3
Floris Velleman
  • 4,848
  • 4
  • 29
  • 46
0
package sample;

import org.apache.commons.exec.util.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;

public class WordSearchCountandposition {

    public static void main(String[] args) {
        RemoteWebDriver driver;

        //driver = new FirefoxDriver();

        System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
        driver = new ChromeDriver();

        driver.manage().window().maximize();
        driver.get("https://jqueryui.com/");

        WebElement Para = driver.findElement(By.xpath("//*[@id='banner-secondary']/p"));
        String ParaCon = Para.getText();

        System.out.println("Paragraph Content : "+ParaCon);

        int ParaConCount = ParaCon.length();
        System.out.println("Paragraph Characters Count : "+ParaConCount);

        int count=0;
        int position=0;

        String WordtoSearch = "a";

        String a[] = ParaCon.split(" ");
        int size = a.length-1;
        System.out.println("Paragraph Words Count : "+size);

        for (int i = 0; i <=size; i++) {
            if (WordtoSearch.equalsIgnoreCase(a[i])) {
                count++;
                position = i;
                System.out.println("Position of Matched Words : "+position);
            }
        }
        System.out.println("Matched Words Count : "+count);

        driver.quit();
    }

}
Vijay
  • 189
  • 1
  • 2
  • 14