Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2009
    Posts
    25
    Rep Power
    0

    Default 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.

    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());
       }
     }
    }
    states.txtdata file attached where the program reads it.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    adz
    adz is offline Member
    Join Date
    Jul 2009
    Posts
    35
    Rep Power
    0

    Default

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by adz View Post
    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.
    He's obviously running his code through a creative indentation format utility, perhaps to obfuscate it and protect the innocent.

  5. #5
    adz
    adz is offline Member
    Join Date
    Jul 2009
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    He's obviously running his code through a creative indentation format utility, perhaps to obfuscate it and protect the innocent.
    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.

  6. #6
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    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)

  7. #7
    Join Date
    Oct 2009
    Posts
    25
    Rep Power
    0

    Default

    I am using Dr.Java and I new to java programming. I need help! thanks

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 04:03 AM
  2. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 08:50 AM
  3. Replies: 2
    Last Post: 10-09-2009, 12:28 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •