I am trying to perform certain calculations, such as sum and percentage, from a text file through Java and have it print the results. I was given an original text file that has a few columns of population data. I successfully read that file and wrote an intermediate file containing only the columns needed through one Java program as seen below:
import java.io.*;
import java.util.Scanner;
public class ReadWrite
{
public static void main(String[] args)
{
try
{
BufferedReader in = null;
in = new BufferedReader(new FileReader("SmallAreaIncomePovertyEstData.txt"));
PrintWriter out = null;
out = new PrintWriter(new BufferedWriter(new
FileWriter("IntermediateSmallAreaIncomePovertyEstData.txt")));
String records = null;
while((records = in.readLine()) != null)
{
String stateCode = records.substring(0, 2);
String districtId = records.substring(4, 8);
String districtName = records.substring(10, 81);
String totalPopulation = records.substring(83, 90);
String schoolAgeChildrenPopulation = records.substring(92, 99);
String povertyPopulation = records.substring(101, 108);
String fileName = records.substring(110, 130);
out.printf("%3s%10s%10s%10s\n", stateCode, totalPopulation,
schoolAgeChildrenPopulation, povertyPopulation);
}
out.close();
}
catch(Exception e)
{
System.out.println("Error writing to file.");
}
}
}
Where I am stuck is now I must read the intermediate file I created and perform some calculations. This is a snippet of the intermediate file:
01 31754 6475 733
01 21522 4010 1563
01 17292 2719 1158
01 9044 1512 471
01 22759 3304 1122
01 8238 1491 250
01 23494 3767 770
01 6019 971 283
01 57441 7120 994
01 55246 10643 1855
01 195540 32652 6171
01 14124 1769 759
01 27510 4765 2009
01 22512 3590 1096
01 212631 30718 12740
01 51246 9201 2218
01 9619 1742 464
01 5354 866 279
01 10639 1565 684
01 20265 3494 1430
01 56502 9945 2162
01 27696 4343 1348
01 26203 4163 1184
There are 56 samples and this snippet just shows a small portion of sample 01. I need to take all the data for sample 01 and add them together. So all the numbers in each of the second, third and fourth columns need to have a sum so that the output for sample 01 would be something like...
01 8368948 747839 87639
And so on. Instead of having pages upon pages of individual data, I would have 56 rows of combined data. I was doing this piece by piece and so far I have the following code:
import java.io.*;
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
try
{
BufferedReader in = null;
in = new BufferedReader(new
FileReader("IntermediateSmallAreaIncomePovertyEstData.txt"));
String records = null;
int totalPop = 0;
String [] population = null;
System.out.println("State Code + Population + Child Population + Child Poverty
Population + % Child Poverty");
System.out.println("========== + ========== + ================ +
======================== + ===============");
while((records = in.readLine()) != null)
{
population = records.split(" ");
totalPop += Integer.parseInt(population[1]);
System.out.println(totalPop);
}
in.close();
}
catch(Exception e)
{
System.out.println("Error writing to file.");
}
}
}
This method is successful until it reaches a number that is smaller or larger than the first. For example, in the data I already showed, I get a successful sum of numbers until I hit 9044 from column 2 since the spacing is different.
01 31754 6475 733
01 21522 4010 1563
01 17292 2719 1158
01 9044 1512 471
I've tried left justifying my intermediate file so all the numbers would line up but that only helps me with column 2. I would encounter the same issue with columns 3 and 4 since the population data is different in each row.
How do I create a method that would add these values together successfully?
To add on to that, I need to take the last 2 columns of summed data and divide them to create a new column of percent population. I figure once I figure out the addition part, this part will be an easy calculation.
Any advice or wisdom would be appreciated! Thank you!!