-2

I'm writing new accounting program for my customer on Android,In some case I need to convert text of EditText to integer,but when I want to parse "-1" the java.lang.NumberFormatException occured!

for understanding my problem try this: int i = Integer.parseInt("-1");

Can anybody help me? Thank you!

this is code:

  int i = Integer.parseInt("-1");

and this is log trace:

07-09 03:33:36.932: E/AndroidRuntime(925):  at dalvik.system.NativeStart.main(Native Method)
07-09 03:36:09.252: E/AndroidRuntime(985): FATAL EXCEPTION: main
07-09 03:36:09.252: E/AndroidRuntime(985): java.lang.NumberFormatException: Invalid int: "-1 
07-09 03:36:09.252: E/AndroidRuntime(985): "
07-09 03:36:09.252: E/AndroidRuntime(985):  at java.lang.Integer.invalidInt(Integer.java:138)
07-09 03:36:09.252: E/AndroidRuntime(985):  at java.lang.Integer.parse(Integer.java:375)
07-09 03:36:09.252: E/AndroidRuntime(985):  at java.lang.Integer.parseInt(Integer.java:366)
07-09 03:36:09.252: E/AndroidRuntime(985):  at java.lang.Integer.parseInt(Integer.java:332)
07-09 03:36:09.252: E/AndroidRuntime(985):  at forms.FYH_moshtari.UpdateRow(FYH_moshtari.java:149)
07-09 03:36:09.252: E/AndroidRuntime(985):  at yhesabyargui.YMaster_Body.OnAccept(YMaster_Body.java:61)
07-09 03:36:09.252: E/AndroidRuntime(985):  at yhesabyargui.YMaster_Body$10.onClick(YMaster_Body.java:321)
07-09 03:36:09.252: E/AndroidRuntime(985):  at yhesabyargui.CYH_Ctrl_ToolItem.onClick(CYH_Ctrl_ToolItem.java:111)
07-09 03:36:09.252: E/AndroidRuntime(985):  at android.view.View.performClick(View.java:4240)
07-09 03:36:09.252: E/AndroidRuntime(985):  at android.view.View$PerformClick.run(View.java:17721)
07-09 03:36:09.252: E/AndroidRuntime(985):  at android.os.Handler.handleCallback(Handler.java:730)
07-09 03:36:09.252: E/AndroidRuntime(985):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-09 03:36:09.252: E/AndroidRuntime(985):  at android.os.Looper.loop(Looper.java:137)

This is complete code:

public interface IYH_BodyControl {
void showError(String Caption);
void showHint(String Caption);
void setNormal();
String getValue();
void setValue(String Value);
void setEnable(Boolean Enable);
void setLabel(String Label);
void setVisible(Boolean Visible);
LinearLayout getFrame();
int getID();
Boolean ValidateForInsert();
Boolean ValidateForUpdate();
Boolean ValidateForDelete();
void ClearContent();
void Focus();
}
....
    protected Hashtable<Integer, IYH_BodyControl> ctrls = new Hashtable<Integer, IYH_BodyControl>();
....
protected void InsertRow() {
    tblmoshtari().setAddress(ctrls.get(ctrladdr).getValue());
    tblmoshtari().setFname(ctrls.get(ctrlfname).getValue());
    tblmoshtari().setFamily(ctrls.get(ctrlfamily).getValue());
    tblmoshtari().setMobile(ctrls.get(ctrlmobile).getValue());
    tblmoshtari().setTel(ctrls.get(ctrltel).getValue());
    tblmoshtari().setMaxDebt(Integer.parseInt(ctrls.get(ctrlmaxdebt).getValue()));
    tblmoshtari().Insert();
}
....

Is it clear?

Yashar Aliabbasi
  • 2,663
  • 1
  • 23
  • 35

2 Answers2

0

I think the log error shows that there is a space after -one-

 java.lang.NumberFormatException: Invalid int: "-1 
 07-09 03:36:09.252: E/AndroidRuntime(985): "

it is like

 Invalid int: "-1 " 

I recommend to do

 String num = "-1";
 Integer.parseInt(num.trim());
Amt87
  • 5,493
  • 4
  • 32
  • 52
0

Please check the the text to see if it contains any invisible characters like white spaces. Try to trim the text before you convert the text to int. int i = Integer.parseInt("-1"); should work without any issues.

Suresh
  • 29
  • 4