1 Replies - 1227 Views - Last Post: 03 October 2012 - 10:24 AM Rate Topic: -----

#1 joseph2324  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 26-April 12

Tokenizing numbers from file

Posted 02 October 2012 - 06:09 PM

The code formatted as 3 weeks each week has its own total sales and average. It is unclear of how to add up grand total of all 3 weeks and grand total of average weekly sales under the column Summary.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;


public class SalesAnalysisClient 
	{
	public static void main(String[] args)
		{
		
		SalesAnalysisService sales = new SalesAnalysisService();
		sales.splitData();
		
		}
}


class SalesAnalysisService
{
	
	public void splitData()
	{
		
		 Scanner scan = new Scanner(System.in);
		 boolean validName = false;
         Scanner fileScan;
        double num1;
        double sum1 = 0;
        double num2;
        double sum2 = 0;
      
         do
         {
           
            System.out.print("Enter file name: ");
           String str = scan.nextLine();   
         
         
            try
            {
            	 
               fileScan = new Scanner (new File(str));
               validName = true;
            
               int count =0;
               
               while (fileScan.hasNext( ))
               {
            	  count++;
            	  
            	   String str1 = fileScan.nextLine();
            	   String [ ]salesAry= str1.split("[,]");
            	  
            	   System.out.println("\t\t\t\tWeek#1 Data");
            	   System.out.println("       \t\t\t-------------------------\n");
            	   System.out.println("\nSales for week1: ");
            	   for(int i =0; i<salesAry.length; i++)
            	   {
            		   num1 =  Double.parseDouble(salesAry[i]);
            		  sum1+=num1;
            		  
            		  
            		   System.out.print("\t" +salesAry[i]);
            		  
            	   }
            	    
            	 
            	  
            	count = salesAry.length;
       			double average1 = sum1/(double)count;
       			
       			System.out.printf ("\n\tTotal Sales for Week 1 = $%6.1f\n\tAverage daily sales for week1 = $%7.1f\n",sum1, average1);
       			
       			
               
               }
               
           //summary
               System.out.println();
               System.out.println("\t\t\t\tSummary");
               System.out.println("       \t\t\t-------------------------\n");
              
           
               double totalForAllWeeks = sum1;
               double averageForAllWeeks = totalForAllWeeks /3;
               System.out.printf ("\n\tTotal Sales for All weeks = $%6.1f\n\tAverage fro all 3 weeks = $%7.1f\n",totalForAllWeeks, averageForAllWeeks);
               
               
            }//end try
            
            catch(FileNotFoundException fnfe)
            {
               System.out.println("Invalid File name; enter again");
            }
      }while(!validName);
   	
   }//end fillAry
	
}//end SalesAnalysisService class 


*Edited: fixed code tags

This post has been edited by pbl: 03 October 2012 - 10:39 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Tokenizing numbers from file

#2 joseph2324  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 26-April 12

Re: Tokenizing numbers from file

Posted 03 October 2012 - 10:24 AM

[]Anybody???[/code]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1