Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java运行时问题:ArrayIndexOutOfBoundsException:索引130超出长度130的界限_Java_Indexoutofboundsexception - Fatal编程技术网

Java运行时问题:ArrayIndexOutOfBoundsException:索引130超出长度130的界限

Java运行时问题:ArrayIndexOutOfBoundsException:索引130超出长度130的界限,java,indexoutofboundsexception,Java,Indexoutofboundsexception,我正在运行以下代码,不断出现以下错误: 线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:Datachange.init(Datachange.java:55)处Datachange.main(Datachange.java:38)的索引130超出长度130的界限 我试图读取一个文件并将其处理成一个输出,但它似乎没有很好地读取该文件。” import java.io.*; public class Datachange { pub

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

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

我试图读取一个文件并将其处理成一个输出,但它似乎没有很好地读取该文件。”

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[13486];

        try (FileReader inputDataframe = new FileReader("SmallAreaIncomePovertyEstData.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("SmallAreaIncomePovertyEstDatanew.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;
    }
}
import java.io.*;
公共类数据更改
{
公共类变量
{
私人机构;
私有int-ID;
私人人口;
私人国际人口儿童;
私人内部人口儿童性别;
私人双人口儿童基金会;
变量(char[]输入)
{
身体=输入;
char[]stateID=java.util.Arrays.copyOfRange(body,0,2);
ID=Integer.parseInt(新字符串(stateID.trim());
char[]totalpopulation=java.util.Arrays.copyOfRange(body,83,90);
population=Integer.parseInt(新字符串(totalpopulation.trim());
char[]childpopulation=java.util.Arrays.copyOfRange(body,92,99);
populationchilds=Integer.parseInt(新字符串(childpopulation.trim());
char[]povertychilds=java.util.array.copyOfRange(body,101108);
populationchildspoverty=Integer.parseInt(新字符串(povertychilds.trim());
}
}
公共静态void main(字符串[]args)
{
Datachange DS=新的Datachange();
DS.init();
}
公共void init()
{
变量dataframe[]=新变量[13486];
try(FileReader inputDataframe=newfilereader(“SmallAreaIncomePovertyEstData.txt”))
{
INTC;
int i=0;
int j=0;
char variableinput[]=新字符[130];
而((c=inputDataframe.read())!=-1)
{
变量输入[i]=(字符)c;
i++;
如果(c==10)
{
dataframe[j]=新变量(variableinput);
j++;
i=0;
}
}
}
捕获(IOException除外)
{
System.out.println(“存在输入/输出错误:+except.getMessage());
}
这个.newdata(数据帧);
}
公共变量[]新数据(变量[]数据集)
{
变量[]新数据=新变量[57];
try(BufferedWriter outData=new BufferedWriter(new
文件编写器(“SmallAreaIncomePovertyEstDatanew.txt”))
{
int stateID=1;//第一个ID
int statePop=0;
int stateChdPop=0;
int stateChdPovertyPop=0;

对于(int i=0;i欢迎使用SO。如果它的ArrayIndexOutOfBound,那么它不是编译时问题,而是运行时问题。我不确定,但问题似乎可以在下面的代码块中

while((c = inputDataframe.read())!=-1)
我认为可能存在问题的原因是,在while块中,您正在递增
variableinput
的指针,而不检查指针索引是否小于130,因此,如果在任何时候输入的字符数超过130,那么您的代码将失败,因为数组的大小是固定的

解决方法是使用
List
而不是
char[]

如果这对你不起作用,请告诉我


祝你好运!

这不是一个“编译问题”,它与或无关。请清楚,不要不加区分地标记。这是一个运行时异常,它是由于数组溢出超过130个元素而导致的。看起来在到达
if(c==10)之前,您已经到达
variableinput
数组的末尾
我会用
StringBuilder
对象替换您的
variableInput
数组。-通常情况下,当您必须选择一些任意大小来放入代码时,您都会出错。@makrandpawar或end of stream。我同意您的诊断。但是,我不建议使用列表来存储字符数据。列表将非常低效。我建议使用某种可调整大小的数组容器。在这里,StringBuilder是一个明显的选择,谢谢各位,我尝试使用列表存储,但它也不起作用。问题是,我正在尝试以尽可能最有效的方式输入/输出文件。@KjAnalytica您已经这样做了通过直接从
文件读取器
一次读取一个字符,而不是将其包装在
缓冲读取器
中而失败。文件I/O很少是速率决定步骤。您可以通过
缓冲读取器.readLine()每秒读取数百万行
。我建议这就足够了。同意@marqueisoflorneagreed@Steve,我们可以更高效地读取文件,包括但不限于使用流读取整个文件并将其存储到列表中。