JAVA问题:索引130超出长度130的范围[重复]

2024-02-03

我正在运行以下代码,但不断收到以下错误:

线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException:索引 130 超出长度 130 的范围 在 Datachange.init(Datachange.java:55) 在 Datachange.main(Datachange.java:38)

我正在尝试读取一个文件并操作为输出,但它似乎没有很好地读取该文件。 '

import java.io.*;

public class Datachange
{
    public class variables
    {
    private char [] body;
    private int ID;
    private int population;
    private int populationchilds;
    private int populationchildspoverty;
    private double populationchildpovertypercent;

        variables(char [] input)
        {
        body = input;

        char[] stateID = java.util.Arrays.copyOfRange(body,0,2);
        ID = Integer.parseInt(new String(stateID).trim());

        char[] totalpopulation  = java.util.Arrays.copyOfRange(body,83,90);
        population = Integer.parseInt(new String(totalpopulation).trim());

        char [] childpopulation = java.util.Arrays.copyOfRange(body,92,99);
        populationchilds = Integer.parseInt(new String(childpopulation).trim());

        char [] povertychilds = java.util.Arrays.copyOfRange(body,101,108);
        populationchildspoverty = Integer.parseInt(new String(povertychilds).trim());
        }
    }




    public static void main(String[] args)
    {
        Datachange DS = new Datachange();
        DS.init();
    }


    public void init()
    {
        variables dataframe[] = new variables[13286];
        try (FileReader inputDataframe = new FileReader("file.txt"))
        {
            int c;
            int i = 0;
            int j = 0;
            char variableinput [] = new char[130];

            while((c = inputDataframe.read())!=-1)
            {

                    variableinput[i] = (char) c;
                    i++;


                if(c==10)
                {
                    dataframe[j] = new variables(variableinput);
                    j++;
                    i = 0;
                }
            }
        }
        catch(IOException except)
        {
            System.out.println("There is Input/Output Error:" + except.getMessage());
        }
        this.newdata(dataframe);
    }


    public variables[] newdata(variables[] dataset)
    {
        variables[] newdata=new variables[57];

        try (BufferedWriter outData = new BufferedWriter(new
                FileWriter("manipulatedfile.txt")))
        {
            int stateID = 1; //First ID
            int statePop= 0;
            int stateChdPop=0;
            int stateChdPovertyPop=0;

            for(int i=0;i<dataset.length;i++)
            {
                if (dataset[i].ID == stateID)
                {
                    statePop += dataset[i].population;
                    stateChdPop += dataset[i].populationchilds;
                    stateChdPovertyPop += dataset[i].populationchildspoverty;
                }

                else
                {
                    double stateChdPovertyPopPercent=0;
                    if (stateChdPop != 0)
                    {
                        stateChdPovertyPopPercent = (double)
                                stateChdPovertyPop/stateChdPop * 100;
                        int z = 12;
                    }

                    else
                    {
                        stateChdPovertyPopPercent = 0;
                    }

                    outData.append(stateID + "\t" + statePop + "\t" +
                            stateChdPop + "\t" + stateChdPovertyPop+
                            "\t" + stateChdPovertyPopPercent + "\n");

                    statePop = 0;
                    stateChdPop = 0;
                    stateChdPovertyPop = 0;
                    i--;
                    stateID++;
                }
            }
        }
        catch(IOException except)
        {
            System.out.println("I/O Error:" + except.getMessage());
        }

        int x = 12;
        return newdata;
    }
}
'

它正在读取文件,否则会抛出一个错误IOException而不是ArrayIndexOutOfBoundsException.

来自Oracle 文档 https://docs.oracle.com/javase/7/docs/api/java/lang/ArrayIndexOutOfBoundsException.html about ArrayIndexOutOfBoundsException:

Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

这发生在你的init()方法如果我没记错的话。您有一个限制为 130 的数组,并且尝试获取索引 130+ 处的元素(因为数组从 0 开始计数)将会出现该异常。

variableinput[130]会扔掉它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JAVA问题:索引130超出长度130的范围[重复] 的相关文章

随机推荐