1

image
I am trying to read numbers in brackets '()' using selenium

The HTML code is

<span class="refinement-count"> (14)</span>

I am trying to read numbers between span.

Using selenium, values in the brackets are stored in a string. After reading the values, I want to add all these values. I used the split function and parsed it into an integer but Integer.parseInt() is throwing the following exception:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""

Here the code:

public static void convert(String s){
     int sum=0;
     String str[]=s.split("[()]+");
     int[] numbers=new int[str.length];

     for (int i = 0; i < str.length; i++)
     {    //System.out.println(str[i]);  --checked here, printing normal integers

          numbers[i]=Integer.parseInt(str[i].trim());
          sum=sum+numbers[i];
     }
     System.out.println("the sum of products is "+sum);
}

Using try-catch() block the exception can be caught but output is not desirable.
Help

MODIFIED

When starting the loop from 1, it is printing/output

 the sum of products is 14
 the sum of products is 8
 the sum of products is 8
 the sum of products is 6
 the sum of products is 4
 the sum of products is 3
 the sum of products is 2
 the sum of products is 2
 the sum of products is 1
 the sum of products is 1
 the sum of products is 1
 the sum of products is 1
 the sum of products is 1
 the sum of products is 1
 the sum of products is 1  

Still there sum is not working

bot13
  • 99
  • 1
  • 15
  • Please share you input string first... – Saurabh Gaur Jul 19 '16 at 12:54
  • Quick guess: the last element in `numbers` is an empty string. Add a check if that is so. – Dakshinamurthy Karra Jul 19 '16 at 12:59
  • It seems, that you pass empty string as argument. – Kirill Jul 19 '16 at 13:00
  • Seems like your input string is empty. – Paras Jul 19 '16 at 13:26
  • i printed the string in the loop, commented out there, it is printing okay – bot13 Jul 19 '16 at 14:29
  • Possible duplicate of [Regex for extracting 3 digits numbers between brackets](http://stackoverflow.com/questions/14591258/regex-for-extracting-3-digits-numbers-between-brackets) – JeffC Jul 19 '16 at 22:40
  • You could save yourself all this scraping and adding and just pull the total off the page. It's right under Best Selling Fairway woods... it says, "54 products found". Grab that, extract the number and you are done. To extract the number split by " " and take the first string. – JeffC Jul 19 '16 at 22:42
  • @JeffC hi sir, it is an exercise where its required to make the sum of all those products and test whether it matches with 54. I am able to extract the number from string " (14)" but not able to add all of them or my extraction is not good. thanks – bot13 Jul 20 '16 at 15:53
  • What is `String s` that you are passing into this function? Is it "CobraGolf (14)" or something else? – JeffC Jul 20 '16 at 16:11
  • string is " (14)" between span tag in html, extracted by findelement.by(xpath).gettext() – bot13 Jul 20 '16 at 16:49

3 Answers3

0

It seems like everyones comment is correct, Why dont you avoid such a case by doing the following:

 for (int i = 0; i < str.length; i++)
 {    //System.out.println(str[i]);  --checked her, printing normal integers
      String numStr = str[i].trim();
      if(numStr != "")
      {
          numbers[i]=Integer.parseInt(numStr);
          sum=sum+numbers[i];
      }
 }
 System.out.println("the sum of products is "+sum);
Moe Ghafari
  • 2,227
  • 1
  • 12
  • 17
0

One solution is to start the index from 1, because when you split the input string the first element in the string array is empty. Another solution is to use string matcher.

0

I would change your function. The function below should be passed "CobraGolf (14)" and would return 14 as an int.

public static int convert(String s)
{
    s = s.replaceAll("\\D+", ""); // removes all characters other than digits
    return Integer.parseInt(s);
}

You would use it like

List<WebElement> filters = driver.findElements(locator); // whatever returns all the filter elements
int sum = 0;
for (WebElement filter : filters)
{
    sum += convert(filter.getText());
}
System.out.println(sum);
JeffC
  • 22,180
  • 5
  • 32
  • 55