Alex Rivera | Logout

How to parse string containing negative number by j2me api?

Asked 2012-02-02T13:16:34.913
9

I have a string which has numbers. I have to parse this string and store these numbers in int, float, etc. Accordingly

String str = "100,2.0,-100,19.99,0";

I can do it by Integer.parseInt() and Float.parseFloat() after splitting. But I can't do it for negative number. It throws exception java.lang.NumberFormatException. After searching web I couldn't find any solution for this problem.

So how can I parse a negative integer from string and store into int using j2me api set?

Edit
Report

2 Answers

2

I had a similar problem today and the problem was that the minus sign in the input string was actually an m-dash character. That was nasty! So that's definitely worth checking.

answered 2013-03-22T22:01:17.833
0

You can split by the delimiter ',' and check if there is a negative sign so multiply the number by -1 or do it by

Float parseFloat(str);

It will work properly.

answered 2012-11-16T13:33:29.937

Your Answer