java - NumberFormatException:对于输入字符串: ""?

标签 java exception string-parsing numberformatexception

代码

int weight = 0;
do {
    System.out.print("Weight (lb): ");
    weight = Integer.parseInt(console.nextLine());
    if (weight <= 0) {
        throw new IllegalArgumentException("Invalid weight.");
    }
} while (weight <= 0);

回溯

Weight (lb): Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.base/java.lang.Integer.parseInt(Integer.java:662)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)
    at HealthPlan.main(HealthPlan.java:46)

当我运行程序时,出现此异常。我该如何处理?

我想输入一个整数 weight值(value)。我还必须使用 height 的整数值,但我的程序要求输入 booleancharacter也是如此。

有人建议我应该使用Integer.parseInt

如果我需要发布更多代码,我很乐意这样做。

最佳答案

有时,它仅仅意味着您将空字符串传递给 Integer.parseInt():

String a = "";
int i = Integer.parseInt(a);

关于java - NumberFormatException:对于输入字符串: ""?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51042662/

相关文章:

Java AES : No installed provider supports this key: javax. crypto.spec.SecretKeySpec

java - 是否可以在整个类上添加 @Secured 或 @PreAuthorized 注释

java - Java 中的 sscanf 等价物

c# - 条件逻辑字符串的解析和正则表达式

java - 如何用多个分隔符拆分字符串 - 并知道哪个分隔符匹配

java - 在java中的线程之间来回发送对象?

java - 如何在使用 EZ-Vcard 编写的 Vcard 中包含字符集?

java - 从未使用过的变量

c++ - 异常继承

c++ - 特征行 vector 超出范围会产生 “Run-Time Check Failure #2 - Stack around the variable X was corrupted”