使用Integer.parseInt()无法将字符串数组转换为整数数组

时间:2016-04-24 01:22:10

标签: java android arrays

我从HTTP服务器得到的响应是一个包含以逗号分隔的值的字符串。我使用逗号作为分隔符在客户端分隔这些值。但是我需要将它转换为整数才能在我的函数中使用它。我使用了Integer.parseInt,但它没有转换。任何人都可以帮忙。

这是从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]);

我得到的输出是:

04-23 18:07:32.898:I / System.out(31596):回复是1,1,15876.0

04-23 18:07:33.867:I / System.out(31596):回复是1,0,15876.0

04-23 18:07:34.773:I / System.out(31596):回复是1,0,15876.0

04-23 18:07:35.765:I / System.out(31596):回复是3,1,5929.0

04-23 18:07:36.820:I / System.out(31596):回复是3,1,5625.0

第二个sysout无效。任何帮助都非常明显。

1 个答案:

答案 0 :(得分:1)

15876.0不是整数。您应该已收到NumberFormatException。

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

如果你只需要整数部分。

相关问题