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
答案 0 :(得分:8)
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: ""
如果您有isEmpty()
或String s
的任意null
,那么您必须拥有特殊代码来处理它,因为Integer.parseInt(s)
将始终抛出异常在那些情况下。
当Integer.parseInt(s)
为s
时,"xyz"
当然可以抛出NumberFormatException
。 try-catch
,因此您可能希望将语句放在String s = ...;
if (s == null || s.isEmpty()) {
complaintAboutNotGettingAnything();
} else {
try {
int num = Integer.parseInt(s);
doSomethingWith(num);
catch (NumberFormatException e) {
complaintAboutGettingSomethingYouDontWant();
}
}
块中。
所以你可以这样写:
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();
}
在这一个表达中,很多事情都可能出错。我建议重构将这一点分解为逻辑可观察的步骤如下:
{{1}}
答案 1 :(得分:0)
我不确定Java,但是如果你有可能不是有效的字符串的字符串 你可能有像C#中的函数:
bool res = Int.TryParse(s,ref int);