【问题标题】:java.lang.NumberFormatException:java.lang.NumberFormatException:
【发布时间】:2019-01-19 17:07:05
【问题描述】:
Integer value = Rs 5000

读取该整数值并将其按空格分割,以便我只能获得整数值。但是当我执行脚本时它显示“5000”。为什么会这样。

日志错误:

java.lang.NumberFormatException:对于输入字符串:“5000 在 java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在 java.lang.Integer.parseInt(Integer.java:580)

int claimAmount= Integer.parseInt(getText(MyClaimsObject.estClaimAmount).split( " ")[1]);
System.out.println(claimAmount);

【问题讨论】:

  • 我刚刚尝试使用硬编码值,它对我来说工作正常。仔细检查 getText 是否返回您认为的内容。
  • 请添加您正在使用的getText(...)方法
  • 引用“Rs 5000”字符串

标签: java selenium appium


【解决方案1】:

推测:有前导或尾随空格。最简单的解决方案(我看到的),使用正则表达式从输入中去除非数字。喜欢,

String claimStr = getText(MyClaimsObject.estClaimAmount).replaceAll("\\D+", "");
int claimAmount = Integer.parseInt(claimStr);

【讨论】:

    【解决方案2】:

    您可能有一些尾随 5000 值的内容,例如不可打印的 UTF 字符。异常文本应为 For input string: "5000",您缺少结束 " 字符。

    确保您正在清理用户输入,例如通过在输入中禁止非数字字符。

    【讨论】:

      猜你喜欢
      • 2020-07-18
      • 1970-01-01
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-11
      • 2020-10-29
      • 1970-01-01
      相关资源
      最近更新 更多