Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.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问题:索引130超出长度130的界限_Java_File_Input_Output - Fatal编程技术网

JAVA问题:索引130超出长度130的界限

JAVA问题:索引130超出长度130的界限,java,file,input,output,Java,File,Input,Output,我正在运行以下代码,不断出现以下错误: 线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:索引130超出长度130的界限 在Datachange.init(Datachange.java:55) 在Datachange.main(Datachange.java:38) 我试图读取一个文件并将其处理为输出,但它似乎无法很好地读取该文件。 " import java.io.*; 公共类数据更改 { 公共类变量 { 私人机构; 私有int-ID;

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

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

我试图读取一个文件并将其处理为输出,但它似乎无法很好地读取该文件。 "

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[]=新变量[13286];
try(FileReader inputDataframe=newfilereader(“file.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
FileWriter(“manufacturedfile.txt”))
{
int stateID=1;//第一个ID
int statePop=0;
int stateChdPop=0;
int stateChdPovertyPop=0;

对于(int i=0;i而言,它读取文件的效果很好,否则它将抛出
IOException
,而不是
ArrayIndexOutOfBoundsException

从about
阵列索引OutofBoundsException

抛出以指示已使用非法索引访问数组。索引为负数或大于或等于数组的大小。

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


variableinput[130]
将抛出它。

variableinput[130]=(char)c;i++;我尝试了这个,但它仍然给出了error@KjAnalytica尝试了为什么?这不是他建议的。看起来在到达
if(c==10)之前,您已经到达
variableinput
数组的末尾
@makrandpawar或流结束。
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;
    }
}
'