I have a file called SalesData.txt containing the dollar amount of sales that a retail store made each day for a number of weeks. Each line in the file contains seven numbers, which are the daily sales for one week. The numbers are separated by a comma. The following is an example from the file:
1245.67,1490.07,1679.87,2371.46,1783.92,1461.99,2059.77
2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36
2513.45,1963.22,1568.35,1966.35,1893.25,1025.36,1128.36
I have converted the String to Double and am trying to calculate:
- The total sales for each week
- The average daily sales for each week
- The total sales for all the weeks
- The average weekly sales
My question is how can Java detect the first line as week 1, the second line as week 2 etc. And calculate the amount. Below is the syntax:
import java.io.*;
import java.util.*;
import java.lang.*;
public class Q1
{
public static void main (String [] args) throws IOException, NumberFormatException
{
//Open the text file
File file = new File("SalesData.txt");
Scanner inputFile = new Scanner (file);
//Read the text file
while (inputFile.hasNext())
{
String weekly_Sale = inputFile.nextLine();
String value = weekly_Sale;
//Split the text by ","
StringTokenizer strtk = new StringTokenizer(value, ",");
//Define array list
ArrayList<String> numList = new ArrayList<String> ();
while (strtk.hasMoreTokens ())
{
numList.add(strtk.nextToken());
}
//Change the data type from String to Double
for (String item:numList)
{
Double result = Double.parseDouble(item);
//System.out.println(result);
}
weekly(result);
}
//Close the text file
inputFile.close();
}
// not sure how to detect the week number
public static void weekly(String numList)
{
for (int i=0; i<numList.size; i++)
{
System.out.println(numList.get(i));
}
}
}