MJ123 MJ123 - 7 months ago 9
Java Question

Convert string array to integer array using Integer.parseInt() not possible

I got response from HTTP server as a string containing values separated by commas. I am separating those values at the client side by using comma as delimiter. But I need to convert it into integer in order to use it in my function. I used Integer.parseInt, but it is not converting.Can anyone please help.

This is the code which gets response from httpserver

HttpResponse response = client.execute(post);
String responseStr = EntityUtils.toString(response.getEntity());
System.out.println("Response is " + responseStr);
separated = responseStr.split(",");
int[] numbers = new int[separated.length];
for(int i = 0;i < separated.length;i++)
numbers[i] = Integer.parseInt(separated[i]);
System.out.println(separated[0] + "and " + separated[1] + "and " + separated[2]);


the output which I am getting is :

04-23 18:07:32.898: I/System.out(31596): Response is 1,1,15876.0

04-23 18:07:33.867: I/System.out(31596): Response is 1,0,15876.0

04-23 18:07:34.773: I/System.out(31596): Response is 1,0,15876.0

04-23 18:07:35.765: I/System.out(31596): Response is 3,1,5929.0

04-23 18:07:36.820: I/System.out(31596): Response is 3,1,5625.0

The second sysout is not working. any help is really appreciable.

Answer

15876.0 is not an integer. You should have received a NumberFormatException.

You could use numbers[i] = (int)Float.parseFloat(separated[i]); 

if you just need the integer part.

Comments