JAVA数组索引超出界限,即使<数组长度

huangapple go评论77阅读模式
英文:

JAVA Array index out of bounds even though < array length

问题

The Array containing the object's details is out of bounds. It would say "Index 1 out of bounds for length 1". However When I printing the strings and arrays and it seems to be captured. Even the array lengths are 3 not 1, but still the error persists

Also, initially, I was able to make this work by putting columns.split("\s+") but after a few runs it went back to this Index 1 out of bounds for length 1....
See Image to see how the problem looks.

What did I miss?

Added Println statement to see what it reads per line.

英文:

The Array containing the object's details is out of bounds. It would say "Index 1 out of bounds for length 1". However When I printing the strings and arrays and it seems to be captured. Even the array lengths are 3 not 1, but still the error persists

Also, initially, I was able to make this work by putting columns.split("\s+") but after a few runs it went back to this Index 1 out of bounds for length 1....
See Image to see how the problem looks.

What did I miss?

Added Println statement to see what it reads per line.

答案1

得分: 0

在while循环中,您应该在拆分之前打印该行,以便您知道该行包含什么。

正如Nick已经建议的那样,也许该行是空的,或者它不包含您期望的FIELD_SEP。

英文:

In the while loop, you should print the line before you split it, so that you know what the line contains.

As Nick already suggested, maybe the line is blank or it does not have the FIELD_SEP as per what you expect.

答案2

得分: 0

我认为您需要检查您的conversionsFile文件,可能在该文件的某处,会有一行只包含一个值/列(即不是正确的数据),而不是包含所有三个值。

英文:

I think you need to check your conversionsFile and somwhere in that file, there might be a line where it will be having only one value/column (i.e not a proper data) instead of having all the three values.

答案3

得分: 0

这行代码出错了:

String toUnit = columns[1];

我认为这个索引处的值不存在,即数组大小为1。

可能的根本原因: 拆分没有正确进行,或者如果你正在拆分这个值,可能只提取了一个值。

检查文件,确保你是否已经设置了这个值,这样它应该在数组中存在。否则,你将会得到那个错误。

英文:

This line is giving error :

String toUnit = columns[1];

I think the value at this index doesn't exist, i.e, the array size is 1.

Possible Root cause : The split is not properly happening or if you are splitting the value, only one value is extracted.

Check the file whether you have set the value or not so that it should be there in the array. Otherwise, you will get that error.

huangapple
  • 本文由 发表于 2020年8月1日 10:45:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63201255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定