【问题标题】:Exception in thread "main" java.lang.NumberFormatException: For input string: "-79%"线程“main”java.lang.NumberFormatException 中的异常:对于输入字符串:“-79%”
【发布时间】:2021-07-29 09:36:07
【问题描述】:

在将折扣值 79% 转换为 79 时出错。

**for (int i = 1; i

        String str=DiscountValue;
        System.out.println(str.substring(1,3));
        
        int Discount = Integer.parseInt(DiscountValue);
        
        if (Discount >= 85) {
            String GameName=driver.findElement(By.xpath("/html[1]/body[1]/div[3]/main[1]/section[1]/div[1]/div[1]/ul[1]/li[3]/div[1]/a[1]/section[1]/span[1]")).getText();
            System.out.println("Please add to Wishlist:"+ GameName);
        } else {
            System.out.println("Jump to next game" +i);
        }** 

【问题讨论】:

  • 您好,您的代码格式不正确,请在发布前查看您帖子的预览。并且还建议添加您正在使用的语言的标签。无论如何,祝你有美好的一天!

标签: selenium parsing selenium-webdriver


【解决方案1】:

您的转换似乎不正确,这就是您收到 java.lang.NumberFormatException 的原因。

当我们尝试转换字符串时抛出 NumberFormatException 转换为浮点数或整数等数值,但格式 输入字符串不合适或非法。

您使用的System.out.println(str.substring(1,3)); 对实际值没有影响,因为String 是不可变的。另外,您需要通过strint Discount = Integer.parseInt(str);

要解决这个问题,您需要使用以下代码:

完整代码:

 String str = DiscountValue;
 str = str.substring(1, 3);
 int Discount = Integer.parseInt(str);

其他将 String 解析为 Integer 的方法是:

str = str.replaceAll("[^\\d]", "");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 2014-05-27
    • 2014-06-07
    • 1970-01-01
    相关资源
    最近更新 更多