简体   繁体   English

Java Integer.ParseInt错误

[英]Java Integer.ParseInt bug

Initial string = 61440 <CARRE> 150 381 188 419 </CARRE> 初始字符串= 61440 <CARRE> 150 381 188 419 </CARRE>

I've split this string into an array, which now contains the coordinates 我已将此字符串拆分为一个数组,该数组现在包含坐标

String[] coord = t.group(2).split(" ");

The resulting output was : 结果输出为:

les coord est :150 381 188 419
i = 0 et sa valeur est :150
i = 1 et sa valeur est :381
i = 2 et sa valeur est :188
i = 3 et sa valeur est :419

for which I did a for loop: 为此,我做了一个for循环:

formeCoord = new int[coord.length];
formeCoord[i] = Integer.parseInt(coord[i]);

Now I'd expect an output with an int array with all the coordinates. 现在,我期望输出带有所有坐标的int数组。 But instead the output is : 但是,输出是:

Voici la valeur de i =0 et sa valeur int: 0
Voici la valeur de i =1 et sa valeur int: 0
Voici la valeur de i =2 et sa valeur int: 0
Voici la valeur de i =3 et sa valeur int: 419

Here is the for loop : 这是for循环:

for (int i = 0; i<formeCoord.length; i++){
    System.out.println("Voici la valeur de i ="
        + i
        + "et sa valeur int: "
        + formeCoord[i]);
}

Does anyone know what I'm doing wrong? 有人知道我在做什么错吗?

It seems you're creating a new array every iteration, instead of adding to it. 似乎您每次迭代都在创建一个新数组,而不是添加到数组中。

Presumably your code looks like this: 大概您的代码如下所示:

for (int i = 0; i < coord.length; i++)
{
  formeCoord = new int[coord.length];
  formeCoord[i] = Integer.parseInt(coord[i]);
}

You need to change it to: 您需要将其更改为:

formeCoord = new int[coord.length];
for (int i = 0; i < coord.length; i++)
  formeCoord[i] = Integer.parseInt(coord[i]);

If you are looping over the following code... 如果要遍历以下代码...

formeCoord = new int[coord.length];
formeCoord[i] = Integer.parseInt(coord[i]);

you are resetting formeCoord every time apart from the last time it gets run 除了上次运行,您每次都要重置formeCoord

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM