简体   繁体   English

java.lang.ArrayIndexOutOfBoundsException:4

[英]java.lang.ArrayIndexOutOfBoundsException: 4

I am working on extracting values from a tab separated text file into a list in groovy. 我正在从制表符分隔的文本文件中提取值到groovy中的列表中。 But am running into the ArrayIndexOutOfBoundsException . 但是我ArrayIndexOutOfBoundsExceptionArrayIndexOutOfBoundsException

Code

println("Reading File Contents")

def fullArray = new String[31721][4]
def availableArray = new String[1386][2]
def filteredFullArray = new String[1386][5]

String fileContents = new File('beliefs.txt').text
String availableContents = new File('available.txt').text

def count = 0

fileContents.eachLine { line ->

    String[] str
    str = line.split('\t')

    def subCount = 0
    for (subCount; subCount < str.length; subCount++) {
         fullArray[count][subCount] = str[subCount]
    }
    count++
}

beliefs.txt beliefs.txt

1   Azerbaijan  hasOfficialLanguage Azerbaijani_language
2   Augustus    hasChild    Julia_the_Elder
3   Arthur_Aikin    isCitizenOf England
4   Arthur_Aikin    diedIn  London
5   Alexander_III_of_Russia isMarriedTo Maria_Feodorovna__Dagmar_of_Denmark_
6   Alexander_III_of_Russia hasChild    Nicholas_II_of_Russia
7   Alexander_III_of_Russia hasChild    Grand_Duke_Michael_Alexandrovich_of_Russia
8   Alexander_III_of_Russia hasChild    Grand_Duchess_Olga_Alexandrovna_of_Russia
9   Alexander_III_of_Russia hasChild    Grand_Duke_Alexander_Alexandrovich_of_Russia
10  Alexander_III_of_Russia hasChild    Grand_Duke_George_Alexandrovich_of_Russia
...
...
...
31719   Minqi_Li    isKnownFor  Chinese_New_Left
31720   Henry_Bates_Grubb   isKnownFor  Mount_Hope_Estate
31721   Thomas_Kuhn isKnownFor  Paradigm_shift  

Running this gives me the following error. 运行此命令会出现以下错误。

Caught: java.lang.ArrayIndexOutOfBoundsException: 4 java.lang.ArrayIndexOutOfBoundsException: 4 at extractBeliefs$_run_closure1.doCall(extractBeliefs.groovy:19) at extractBeliefs.run(extractBeliefs.groovy:12) 捕获:java.lang.ArrayIndexOutOfBoundsException:4 java.lang.ArrayIndexOutOfBoundsException:4在extractBeliefs $ _run_closure1.doCall(extractBeliefs.groovy:19)在extractBeliefs.run(extractBeliefs.groovy:12)

I am aware of the reason why the above error could occur. 我知道上述错误可能发生的原因。 But since my array does not exceed the last index and since the error is shown to be at the line fileContents.eachLine { line -> , I am unable to find where this is going wrong. 但是由于我的数组没有超过最后一个索引,并且由于错误显示在fileContents.eachLine { line -> ,所以我无法找到问题所在。

Any suggestions in this regard will be highly appreciated. 在这方面的任何建议将不胜感激。

Your initial error is coming from this line (19): 您的最初错误来自此行(19):

fullArray[count][subCount] = str[subCount]

Line 12 is just elevating the exception as it exits the closure. 第12行只是退出退出时引发的异常。 This definitely indicates you have an extra tab on one line... for debugging purposes, try printing the line to the console before you attempt to load it into the array. 这绝对表明您在一行上有一个额外的选项卡...出于调试目的,请在尝试将其加载到阵列之前尝试将其打印到控制台。 That'll help you identify which line has the error. 这将帮助您确定哪一行有错误。

Try splitting with space 尝试与空间分裂

str = line.split('\s+')

instead of 代替

str = line.split('\t')

Better way would be to replace all Multispaces or tabs with the single space first and then split by single space. 更好的方法是先用单个空格替换所有“多空格”或“制表符”,然后再按单个空格分割。

line = line.replace("\\s+/g", " ")
str = line.split('\\s+')

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

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