【问题标题】:Could Not parse java.lang.NumberFormat Exception无法解析 java.lang.NumberFormatException
【发布时间】:2011-03-23 14:16:46
【问题描述】:
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

【问题讨论】:

标签: android parsing int


【解决方案1】:

开启Integer.parseInt

异常消息似乎如下:

07-31 14:48:45.409: INFO/System.out(431): Could not parse
   java.lang.NumberFormatException: unable to parse '' as integer

确实,Integer.parseInt(String) 无法解析空字符串。因此:

int num = Integer.parseInt("");
// throws java.lang.NumberFormatException: For input string: ""

如果你有一个任意的String s,它可以是isEmpty(),甚至是null,那么你必须有特殊的代码来处理它,因为Integer.parseInt(s)在这些情况下总是会抛出异常。

当然Integer.parseInt(s) 可以抛出NumberFormatExceptions 是例如"xyz",因此您可能希望将语句放在 try-catch 块中。

所以你可以这样写:

String s = ...;
if (s == null || s.isEmpty()) {
   complaintAboutNotGettingAnything();
} else {
   try {
     int num = Integer.parseInt(s);
     doSomethingWith(num);
   catch (NumberFormatException e) {
     complaintAboutGettingSomethingYouDontWant();
   }
}

关于编写易于调试的代码

在这个特定的 sn-p 中,parseInt 的调用方式如下:

if ((Integer.parseInt(et1.getText().toString()))<=55) ...

很多事情都可能在这个表达中出错。我建议进行重构,将其分解为逻辑上的可观察步骤,如下所示:

String et1text = et1.getText().toString();
// maybe check if it's empty/null if necessary
// maybe log/inspect what the value of et1text is for debugging

try {
   int et1val = Integer.parseInt(et1text);
   if (et1val <= THRESHOLD) {
      // ...
   }
} catch (NumberFormatException e) {
   moreComplaining();
}

【讨论】:

  • 超出职责范围。不错
【解决方案2】:

我不确定 Java,但如果您有可能不是有效整数的字符串 你可能有一个类似 C# 的函数:

bool res = Int.TryParse(s,ref int);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多