简体   繁体   English

java.lang.NumberFormatException:对于输入字符串:使用“ 2”或“ 5”时为“”

[英]java.lang.NumberFormatException: For input string: “” when using “2” or “5”

I read in a menu choice and typing in any number but 2 & 5 work. 我读了一个菜单选项,然后输入2和5以外的任何数字。

String choice = promptUser(choicePrompt);
try {
      outputInfo(String.format("choice=...%s...",choice));
      int c = Integer.parseInt(choice);
      /* process it */
}catch (NumberFormatException e) {

outputInfo(String.format("choice=%s",choice));
outputInfo(e.toString());
}

public static void outputInfo(String msg)
{
    System.out.printf("\t%s\n",msg);
}

Good output: 好的输出:

    Enter Option: 1
    choice=...1...

Bad Output: 错误的输出:

    Enter Option: 2
    choice=...2...
    choice=2
    java.lang.NumberFormatException: For input string: ""

Update: 更新:

I've hard-coded "2" and it still fails!: 我已经硬编码为“ 2”,但仍然失败!:

String choice = promptUser(choicePrompt);
try {
     choice="2";
     outputInfo(String.format("choice=...%s...",choice));
     int c = Integer.parseInt(choice);
     /* process it */
}catch (NumberFormatException e) {

outputInfo(String.format("choice=%s",choice));
outputInfo(e.toString());
}

Hard-coding "5" also fails but "1" works!!! 硬编码“ 5”也失败,但“ 1”有效!!!

Any ideas gratefully received. 任何想法表示感谢。

Simon 西蒙

If I assume your promptUser() method to be something like: 如果我假设您的promptUser()方法是这样的:

static String promptUser() {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    try {
        return reader.readLine();
    }
    catch(Exception ex) {
        return null;
    }
}

(without the parameter) then the program behaves as expected - certainly there's nothing in that code that treats 2 or 5 differently. (不带参数),则程序将按预期方式运行-当然,该代码中没有任何内容可以区别2或5。 If you're getting an empty string then are you sure your prompt user method is working correctly? 如果您得到一个空字符串,那么您确定提示用户方法可以正常工作吗?

Either way, the code you've posted here is essentially correct. 无论哪种方式,您在此处发布的代码本质上都是正确的。 I would imagine there's something else wrong in your more fully complete program that doesn't manifest itself when you've reduced it down here; 我想您更完整的程序中还有其他错误,当您将其简化为此处时,该错误不会显现出来。 perhaps you're running into a case where a local variable is hiding a field for example and you're not using the value you think you are (but at this point, I'm just guessing.) 也许您遇到了这样的情况:例如,局部变量隐藏了一个字段,而您没有使用您认为的值(但在这一点上,我只是在猜测)。

updated 更新

Seems the promptUser method is returning an empty String "". 似乎promptUser方法正在返回一个空字符串“”。 check if choice is empty before calling ParseInt method 在调用ParseInt方法之前检查选择是否为空

Also you can add trim() to eliminate spaces before and after the input 您也可以添加trim()以消除输入前后的空格

  if(choice!=null && !"".equals(choice))
     int c = Integer.parseInt(choice.trim());

printStackTrace() is your friend. printStackTrace()是您的朋友。 Turns out the number format exception was further down (in the 'process it' code) and was not being caught down there. 事实证明,数字格式异常更进一步(在“ process it”代码中)并且没有被捕获。 It was data driven so didn't happen on other machines. 它是数据驱动的,因此在其他计算机上没有发生。

Thanks to every one for your support. 感谢每一个人的支持。

Simon 西蒙

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM