Could Not parse java.lang.NumberFormat Exception
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.NumberFormatE开发者_如何学Goxception: 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();
}
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:
bool res = Int.TryParse(s,ref int);
精彩评论