I am running the following code and I keep getting the following error:
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[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;
}
}