本文介绍了无法解析java.lang.NumberFormat异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {尝试
    如果(flag_conv == FALSE)
    {
      如果((的Integer.parseInt(et1.getText()的toString()))≤; = 55)
      {
       最后AlertDialog alertDialog =新AlertDialog.Builder(本).create();
       alertDialog.setTitle(复位...);
       alertDialog.setMessage(WB应该是刨丝器超过55);       alertDialog.setButton2(OK,新DialogInterface.OnClickListener(){
          公共无效的onClick(DialogInterface对话,诠释其)
          {
                //这里你可以添加功能
                dialog.dismiss();
          }});
       alertDialog.setIcon(R.drawable.icon);
       alertDialog.show();
       tv1.setText(WB);
       et1.setText();
       wbflg = TRUE;
       wbval = 0;
       返回;
     }
     其他
     {
      wbval =的Integer.parseInt(et1.getText()的toString());
     }
   }
 赶上(NumberFormatException的NFE)
{的System.out.println(无法解析+ NFE);}

和我得到了以下异常

  07-31 14:48:45.409:信息/的System.out(431):无法解析java.lang.NumberFormatException:无法解析''作为整数
07-31 14:48:50.569:信息/的System.out(431):无法解析java.lang.NumberFormatException:无法解析''作为整数
07-31 14:48:54.599:信息/的System.out(431):无法解析java.lang.NumberFormatException:无法解析''作为整数
07-31 14:48:54.829:信息/的System.out(431):无法解析java.lang.NumberFormatException:无法解析''作为整数
07-31 14:48:54.958:信息/的System.out(431):无法解析java.lang.NumberFormatException:无法解析''作为整数
07-31 14:48:55.108:信息/的System.out(431):无法解析java.lang.NumberFormatException:无法解析''作为整数
07-31 14:48:55.259:信息/的System.out(431):无法解析java.lang.NumberFormatException:无法解析''作为整数
07-31 14:48:55.409:调试/ dalvikvm(118):GREF已增加至201
07-31 14:48:55.429:信息/的System.out(431):无法解析java.lang.NumberFormatException:无法解析''作为整数
07-31 14:52:43.798:DEBUG / SntpClient(58):请求时失败:java.net.SocketException异常:地​​址协议不支持


解决方案

的Integer.parseInt

异常消息似乎是以下几点:

  07-31 14:48:45.409:信息/的System.out(431):无法解析
   java.lang.NumberFormatException:无法解析''作为整数

事实上,一个空字符串不能由<$c$c>Integer.parseInt(String).因此:

  INT NUM =的Integer.parseInt();
//抛出java.lang.NumberFormatException:对于输入字符串:

如果你有一个任意的String 可以是<$c$c>isEmpty()甚至,那么你必须有特殊的code来处理它,因为的Integer.parseInt(S)总是会抛出一个异常,在这种情况下。

当然,的Integer.parseInt(S)可以抛出<$c$c>NumberFormatException取值是如XYZ,所以你可以把它放到语句中的的try-catch 块中。

所以,你可以这样写:

 一个String = ...;
如果(S == NULL || s.isEmpty()){
   complaintAboutNotGettingAnything();
}其他{
   尝试{
     INT NUM =的Integer.parseInt(S);
     doSomethingWith(NUM);
   赶上(NumberFormatException的E){
     complaintAboutGettingSomethingYouDontWant();
   }
}


在写作code,易于调试

在这个特殊的代码片段,它看起来像 parseInt函数被调用如下:

(。(的Integer.parseInt(et1.getText()的toString()))&LT; = 55)

 如果...

很多事情可以去错在这其中前pression。我建议重构,除了闯入逻辑观察到这个步骤如下:

 字符串et1text = et1.getText()的toString()。
//也许检查它是否是空/空,如果有必要
//也许日志/检查什么et1text的价值进行调试尝试{
   INT et1val =的Integer.parseInt(et1text);
   如果(et1val&LT; =阈值){
      // ...
   }
}赶上(NumberFormatException的E){
   moreComplaining();
}
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
解决方案

On Integer.parseInt

The exception message seems to be the following:

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

Indeed, an empty string can not be parsed by Integer.parseInt(String). Thus:

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

If you have an arbitrary String s which can be isEmpty() or even null, then you must have special code to handle it, because Integer.parseInt(s) will always throw an exception in those cases.

Of course Integer.parseInt(s) can throw NumberFormatException when s is e.g. "xyz", so you may want to put the statement inside a try-catch block.

So you can write something like this:

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


On writing code that is easy to debug

In this particular snippet, it looks like parseInt is invoked as follows:

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

A lot of things can go wrong in this one expression. I suggest refactoring that breaks this apart into logical observable steps as follows:

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();
}

这篇关于无法解析java.lang.NumberFormat异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 17:29