Application Error in Parsing String to Integer

Put your problem here if it does not fit any of the other categories.

Application Error in Parsing String to Integer

Postby ramgraph1 » Fri May 02, 2008 2:40 pm

I am using code like this below to convert a button's text to an integer. When I run it I get an "Application Error" but in the error dialog, at the end, the number from the button is displayed. Could someone tell me what is causing the error and point me in the direction of the proper way of fixing it?

Code: Select all
int number = Integer.parseInt(button1.getText().toString());


Thanks in advance.
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Top

Postby cadlg » Fri May 02, 2008 3:16 pm

Hi ramgraph1,

1) Have you tried to catch NumberFormatException ?
2) It would be useful to see the complete error message...

By the way, I always use Integer.valueOf(String string) instead of Integer.parseInt, although I guess the result
should be the same

Regards,

cadlg
cadlg
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Feb 20, 2008 12:33 am
Location: Guatemala

Postby ramgraph1 » Fri May 02, 2008 11:11 pm

Thanks for your reply cadlg. I tried Integer.valueOf and get the same result. The full error message is just "Application Error: (name of my app) An error has occurred in (name of my app). 8."
With the "8" being the integer that is getting returned as the result of the problematic code...
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Postby ramgraph1 » Fri May 02, 2008 11:23 pm

Found the issue. There was a line break "\n" before the number in the String that I was converting. I took that out and it works fine.
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Postby cadlg » Fri May 02, 2008 11:27 pm

Good news !! :)

take care,

cadlg
cadlg
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Feb 20, 2008 12:33 am
Location: Guatemala

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: MSNbot Media and 19 guests