/******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
import java.io.*;
public class Main {
public static void main(String[] args){
DataManager.main(args);
}
}
class DataManager {
public static void main(String[] args) {
DataManager dataManager = new DataManager();
dataManager.init();
}
public void init() {
Row[] dummyDataSet = new Row[13486]; //dummy array
char[] dummyCharArray = new char[13486]; //dummy char array
for(int i = 0; i < dummyCharArray.length; i++){
dummyCharArray[i] = '5';
}
dummyDataSet[1] = new Row(dummyCharArray); // dummy initializatiol of Row index
// manipulateData(dummyDataSet);
Row dataSet[] = new Row[13486]; // number of rows from data
try ( FileReader fr = new FileReader("SmallAreaIncomePovertyEstData.txt")) // this is used to read the data from the .txt file
{
int c;
int i = 0;
int j = 0;
char rowInputs[] = new char [130];
while((c = fr.read()) != -1) {
rowInputs[i] = (char) c;
i++;
if ( c == 10) { // `32` is blank space, '10' is new line
dataSet[j] = new Row(rowInputs);
j++;
i = 0;
}
}
} catch (IOException e) {
System.out.println(e + "occured");
}
// manipulate the data
this.manipulateData(dataSet);
// write the data
}
public Row[] manipulateData(Row[] dataset) {
Row[] manipulatedData = new Row[57];
char rowInputs[] = new char [130];
try ( BufferedWriter bw = new BufferedWriter (new FileWriter("SmallAreaIncomePovertyEstDataManipulated.txt")) )
{
int stateId = 1; // starting point id
int statePopulation = 0;
int stateChildPopulation = 0;
int stateChildPovertyPopulation = 0;
for (int i = 0; i < dataset.length; i++) {
dataset[i] = new Row(rowInputs,stateId, statePopulation, stateChildPopulation, stateChildPovertyPopulation);
if (dataset[i].id == stateId) {
statePopulation += dataset[i].population;
stateChildPopulation += dataset[i].childPopulation;
stateChildPovertyPopulation += dataset[i].childPovertyPopulation;
} else {
double stateChildPovertyPopulationPercentage = 0;
if (stateChildPopulation != 0) {
stateChildPovertyPopulationPercentage = (double) stateChildPovertyPopulation/stateChildPopulation*100;
int z = 12;
} else {
stateChildPovertyPopulationPercentage = 0;
}
bw.append(stateId + " \t " + statePopulation + " \t " + stateChildPopulation + " \t " + stateChildPovertyPopulation + " \t " + stateChildPovertyPopulationPercentage + "\n");
statePopulation = 0;
stateChildPopulation = 0;
stateChildPovertyPopulation = 0;
i--;
stateId++;
}
}
} catch (IOException e) {
System.out.println("I/O Error: " + e);
}
int x = 12;
return manipulatedData;
};
class Row {
private char[] contents;
private int id;
private int population;
private int childPopulation; // Pop of children 5 to 17
private int childPovertyPopulation; // this is the estimated number of children in poverty
private double percentageChildPovertyPopulation;
// in Poverty Related to the Householder
//dummy constructor
public Row(char[] contents, int id, int population, int childPopulation, int childPovertyPopulation){
this.contents = contents;
this.id = id;
this.population = population;
this.childPopulation = childPopulation;
this.childPovertyPopulation = childPovertyPopulation;
this.percentageChildPovertyPopulation = percentageChildPovertyPopulation;
}
Row (char[] input) {
contents = input;
char [] stateIdPlaceholder = java.util.Arrays.copyOfRange(contents, 0, 2); // state id [char range]
id = Integer.parseInt(new String(stateIdPlaceholder).trim());
char[] tPopPlaceholder = java.util.Arrays.copyOfRange(contents, 83, 90); // total poulation [char range]
population = Integer.parseInt(new String(tPopPlaceholder).trim());
char[] cPopPlaceholder = java.util.Arrays.copyOfRange(contents, 92, 99); // child population [char range]
childPopulation = Integer.parseInt(new String(cPopPlaceholder).trim());
char[] cPPopPlaceholder = java.util.Arrays.copyOfRange(contents, 101, 108); // child poverty population [char range]
try{
childPovertyPopulation = Integer.parseInt(new String(cPPopPlaceholder).trim());
} catch (NumberFormatException e) {
System.out.println(e);
}
}
}
}
import java.io.*;
public class DataPresenter {
public static void main(String[] args) {
System.out.println("File: SmallAreaIncomePovertyEstDataManipulated.txt");
System.out.println("State" + " \t " + "Population" + " \t " + "Child Population" + " \t " + "Child Poverity Population" + " \t " + "% Child Poverty");
System.out.println("-------" + " ------------" + " \t " + "------------------" + " \t " + "----------------------------" + " \t " + "-----------------");
// read the data
try ( FileReader fr = new FileReader("SmallAreaIncomePovertyEstDataManipulated.txt"))
{
int c;
while((c = fr.read()) != -1) {
System.out.print((char) c);
}
} catch (IOException e) {
System.out.println("I/O Error: " + e);
}
}
}