java.lang.NumberFormatException:无效INT:""在机器人机器人、lang、java、NumberFormatException

2023-09-07 15:11:43 作者:缺

我已经知道是什么原因造成这个错误,我只是不知道如何处理的情况下,当用户不输入任何东西到对话框,然后点击其中解析串入一个int按钮。它不能解析一个空字符串为一个int,所以它抛出一个错误。我已经做就怎么做这方面的一些研究,但还没有发现工作满意的结果。

I already know what is causing this error, I just do not know how to handle the case when a user doesn't enter anything into the dialogue box, then hit the button which parses the string into an int. It can't parse an empty string into an int, so it throws an error. I have done some research on how to do this, but have not found a satisfactory result that works.

问题:你如何检查,看看是否该对话框中有文本,之前尝试运行的code中的其余部分。

Problem: How do you check to see if the dialogue box has text in it, before it tries to run the rest of the code.

推荐答案

有些code有助于与语法,但基本上

Some code would help with the syntax but basically

 if ("".equals(text)  // where text is the text that you get from an EditText or wherever you get it
 {    // give message to enter valid text;    }

此外,您还可以用的try / catch包围,赶上一个NumberFormatException异常然后打印相应的消息

Also, you can surround with a try/catch and catch a numberFormatException then print an appropriate message