My problem is that my total is coming as a cumalative, it is adding all the States population, so how do I break the calculation when another State starts, suppose NY state start at row = 5, so it should add till that. And for NJ it should add from row6 to row 9, and same for CT state.
Attachment 723data file attached where the program reads it.Code:import java.io.*;
import java.text.*;
import java.util.StringTokenizer;
public class statepop {
public static void main(String[] args) {
// System.out.println("Memo# 458");
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
java.util.Date date = new java.util.Date();
String datetime = dateFormat.format(date);
System.out.println(" \t\t\t\tDate " + datetime);
System.out.println("**--------------------------------------------------** ");
System.out.println("State \tCity \tM Pop.\tF Pop. \tChild Pop.");
System.out.println("**--------------------------------------------------** ");
try {
FileInputStream fstream =
new FileInputStream("C:\\Users\\FK\\Desktop\\states.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
int rows = 0;
float totalma = 0;
float totalfe = 0;
float totalch = 0;
// float avg;
// float avg1,sum;
//double gt;
// double tax;
double malep = 1;
double femalep = 1;
double childp=1;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
rows++;
// Print the content on the console
StringTokenizer Tok = new StringTokenizer(strLine, ",");
int currentToken = 0;
int totalTokens = Tok.countTokens();
while (Tok.hasMoreElements()) {
currentToken++;
String tokenValue = Tok.nextToken();
if (currentToken == 3) malep = Double.parseDouble(tokenValue);
if (currentToken == 4) femalep = Double.parseDouble(tokenValue);
if (currentToken == 5) childp = Double.parseDouble(tokenValue);
System.out.print(tokenValue + "\t");
if(currentToken==totalTokens){
totalma+=malep;
totalfe+=femalep;
totalch+=childp;
if(rows ==5){
System.out.println("\n___________________________________________________________");
System.out.printf("\nTotal\t\t%.0f", totalma);
System.out.printf("\t%.0f", totalfe);
System.out.printf("\t%.0f", totalch);
System.out.println("\n___________________________________________________________");}
if (rows ==9){
System.out.println("\n___________________________________________________________");
System.out.printf("\nTotal\t\t%.0f", totalma);
System.out.printf("\t%.0f", totalfe);
System.out.printf("\t%.0f", totalch);
System.out.println("\n___________________________________________________________");}
// System.out.print(malep);
// System.out.print(femalep);
// System.out.print(childp);
}
}
System.out.print("\n");
}
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ");
System.out.printf("Total\t\t%.0f", totalma);
System.out.printf("\t%.0f", totalfe);
System.out.printf("\t%.0f", totalch);
//Close the input stream
in.close();
} catch (Exception e) { //Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}

