gpt4 book ai didi

java - 尝试在 Selenium 中读取跨度之间的文本时出现数字格式异常

转载 作者:太空宇宙 更新时间:2023-11-04 12:25:10 25 4
gpt4 key购买 nike

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

HTML 代码为

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

我正在尝试读取跨度之间的数字。

使用selenium,括号中的值存储在字符串中。读取这些值后,我想添加所有这些值。我使用 split 函数并将其解析为整数,但 Integer.parseInt() 抛出以下异常:

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

代码如下:

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);
}

使用 try-catch() block 可以捕获异常,但输出并不理想。
帮助

已修改

从1开始循环时,是打印/输出

 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

仍然有总和不起作用

最佳答案

看来大家的评论都是对的,为什么不通过执行以下操作来避免这种情况:

 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);

关于java - 尝试在 Selenium 中读取跨度之间的文本时出现数字格式异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38459123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com