无法解析 java.lang.NumberFormat 异常
try{
if (flag_conv == false)
{
if ((Integer.parseInt(et1.getText().toString()))<=55)
{
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Reset...");
alertDialog.setMessage("WB should be grater than 55");
alertDialog.setButton2("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
// here you can add functions
dialog.dismiss();
}});
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
tv1.setText("WB");
et1.setText("");
wbflg = true;
wbval = 0;
return;
}
else
{
wbval = Integer.parseInt(et1.getText().toString());
}
}
catch(NumberFormatException nfe)
{System.out.println("Could not parse " + nfe);}
我得到了以下异常
07-31 14:48:45.409: INFO/System.out(431): Could not parse java.lang.NumberFormatException: unable to parse '' as integer
07-31 14:48:50.569: INFO/System.out(431): Could not parse java.lang.NumberFormatException: unable to parse '' as integer
07-31 14:48:54.599: INFO/System.out(431): Could not parse java.lang.NumberFormatException: unable to parse '' as integer
07-31 14:48:54.829: INFO/System.out(431): Could not parse java.lang.NumberFormatException: unable to parse '' as integer
07-31 14:48:54.958: INFO/System.out(431): Could not parse java.lang.NumberFormatException: unable to parse '' as integer
07-31 14:48:55.108: INFO/System.out(431): Could not parse java.lang.NumberFormatException: unable to parse '' as integer
07-31 14:48:55.259: INFO/System.out(431): Could not parse java.lang.NumberFormatException: unable to parse '' as integer
07-31 14:48:55.409: DEBUG/dalvikvm(118): GREF has increased to 201
07-31 14:48:55.429: INFO/System.out(431): Could not parse java.lang.NumberFormatException: unable to parse '' as integer
07-31 14:52:43.798: DEBUG/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol
try{
if (flag_conv == false)
{
if ((Integer.parseInt(et1.getText().toString()))<=55)
{
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Reset...");
alertDialog.setMessage("WB should be grater than 55");
alertDialog.setButton2("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
// here you can add functions
dialog.dismiss();
}});
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
tv1.setText("WB");
et1.setText("");
wbflg = true;
wbval = 0;
return;
}
else
{
wbval = Integer.parseInt(et1.getText().toString());
}
}
catch(NumberFormatException nfe)
{System.out.println("Could not parse " + nfe);}
And i got the following Exception
07-31 14:48:45.409: INFO/System.out(431): Could not parse java.lang.NumberFormatException: unable to parse '' as integer
07-31 14:48:50.569: INFO/System.out(431): Could not parse java.lang.NumberFormatException: unable to parse '' as integer
07-31 14:48:54.599: INFO/System.out(431): Could not parse java.lang.NumberFormatException: unable to parse '' as integer
07-31 14:48:54.829: INFO/System.out(431): Could not parse java.lang.NumberFormatException: unable to parse '' as integer
07-31 14:48:54.958: INFO/System.out(431): Could not parse java.lang.NumberFormatException: unable to parse '' as integer
07-31 14:48:55.108: INFO/System.out(431): Could not parse java.lang.NumberFormatException: unable to parse '' as integer
07-31 14:48:55.259: INFO/System.out(431): Could not parse java.lang.NumberFormatException: unable to parse '' as integer
07-31 14:48:55.409: DEBUG/dalvikvm(118): GREF has increased to 201
07-31 14:48:55.429: INFO/System.out(431): Could not parse java.lang.NumberFormatException: unable to parse '' as integer
07-31 14:52:43.798: DEBUG/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
Integer.parseInt
上,异常消息似乎如下:
事实上,空字符串不能被
Integer.parseInt(String)
。因此:如果您有一个任意的
String
,它可以是isEmpty()
甚至null
,那么你必须有特殊的代码来处理它,因为Integer.parseInt(s) 将始终抛出异常。
当然
Integer.parseInt(s)
可以抛出NumberFormatException
当s
是例如"xyz"
时,因此您可能需要将该语句放在try-catch
块。因此,您可以编写如下内容:
关于编写易于调试的代码
在这个特定的代码片段中,
parseInt
的调用方式如下:在这个表达式中,很多事情都可能出错。我建议重构,将其分解为逻辑可观察的步骤,如下所示:
On
Integer.parseInt
The exception message seems to be the following:
Indeed, an empty string can not be parsed by
Integer.parseInt(String)
. Thus:If you have an arbitrary
String s
which can beisEmpty()
or evennull
, then you must have special code to handle it, becauseInteger.parseInt(s)
will always throw an exception in those cases.Of course
Integer.parseInt(s)
can throwNumberFormatException
whens
is e.g."xyz"
, so you may want to put the statement inside atry-catch
block.So you can write something like this:
On writing code that is easy to debug
In this particular snippet, it looks like
parseInt
is invoked as follows:A lot of things can go wrong in this one expression. I suggest refactoring that breaks this apart into logical observable steps as follows:
我不确定 Java,但如果你有可能不是有效整数的字符串
你可能有一个类似于 C# 中的函数:
I am not sure about Java but if you have strings which might not be valid ints
you may have a function like in C# of: