JAVA问题:索引130超出长度130的范围 [英] JAVA Question: Index 130 out of bounds for length 130

查看:91
本文介绍了JAVA问题:索引130超出长度130的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am running the following code and I keep getting the following error:

线程"main"中的异常; java.lang.ArrayIndexOutOfBoundsException:索引130的长度为130 在Datachange.init(Datachange.java:55) 在Datachange.main(Datachange.java:38)

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 130 out of bounds for length 130 at Datachange.init(Datachange.java:55) at Datachange.main(Datachange.java:38)

我正在尝试读取文件并将其操纵为输出,看来它不能很好地读取文件. '

I am trying to read a file and manipulated into an output and its seems that its not reading the file well. '

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.

It is reading the file just fine, otherwise it would throw an IOException instead of an ArrayIndexOutOfBoundsException.

来自 Oracle文档关于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开始计数)将给您该异常.

That happens in your init() method if I am not mistaken. You have an array that has a limit of 130 and trying to get the element at index 130+ (because arrays start counting at 0) will give you that exception.

variableinput[130]会扔掉它.

这篇关于JAVA问题:索引130超出长度130的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆