Results 1 to 7 of 7
Thread: how "break" calculation
- 01-17-2010, 10:35 PM #1
how "break" calculation
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.
states.txtdata file attached where the program reads it.XML 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()); } } }
-
You should create a class that holds one state's information in it, and have an ArrayList of these objects. Then when you read each line, first extract the state, see if you already an object for this state in your list, if not create one, and if so, add your numbers to it.
- 01-17-2010, 10:57 PM #3
Member
- Join Date
- Jul 2009
- Posts
- 35
- Rep Power
- 0
Also I'd recommend you use some kind of layout for your code or an IDE formatter and also use methods. That code is pretty horrible to look at like this.
-
- 01-17-2010, 11:02 PM #5
Member
- Join Date
- Jul 2009
- Posts
- 35
- Rep Power
- 0
I occasionally look back at code I wrote in my first year of uni (I've only been working as a programmer for 6 months so still only 4 years ago) and thinking what the hell is this. Which someone had slapped me in the face, gave me an IDE and told me to format it properly.
- 01-17-2010, 11:17 PM #6
eclipse all the way. ;)
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 01-17-2010, 11:52 PM #7
Similar Threads
-
Java, Military Format using "/" and "%" Operator!!
By sk8rsam77 in forum New To JavaReplies: 11Last Post: 02-26-2010, 03:03 AM -
problem with argument list and precedence "(" and ")"
By helpisontheway in forum Advanced JavaReplies: 6Last Post: 12-24-2009, 07:50 AM -
how to override "cancel operation" in "progress bar"
By singswt in forum SWT / JFaceReplies: 2Last Post: 10-08-2009, 11:28 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks