无法解析java.lang.NumberFormat异常

bin*_*dal 0 int parsing android

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);}
Run Code Online (Sandbox Code Playgroud)

我得到以下例外

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
Run Code Online (Sandbox Code Playgroud)

pol*_*nts 8

Integer.parseInt

异常消息似乎如下:

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

实际上,无法解析空字符串Integer.parseInt(String).从而:

int num = Integer.parseInt("");
// throws java.lang.NumberFormatException: For input string: ""
Run Code Online (Sandbox Code Playgroud)

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

当然Integer.parseInt(s)可以扔NumberFormatException的时候s是例如"xyz",所以你可以把它放到语句中的内部try-catch块.

所以你可以这样写:

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

编写易于调试的代码

在这个特定的片段中,它看起来像parseInt调用如下:

if ((Integer.parseInt(et1.getText().toString()))<=55) ...
Run Code Online (Sandbox Code Playgroud)

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

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();
}
Run Code Online (Sandbox Code Playgroud)