英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论