This is the code I have written so far. I'm trying to get the output right before I save it to file. My for loop feels right but it's iterating 96 times even though there are only 10 lines in the text file.
This is the text in the StoreDailySales.txt file. If I remove the for loop and just use the print statement it will iterate correctly. I feel like there is something wrong with the variable amount /= 100, but I'm not sure.
Code
800.00
1000.00
500.00
2000.00
1800.00
100.00
1000.00
900.00
1000.00
1500.00
Code
package edu.carrollcc.cis132;
import java.io.*;
import java.util.Scanner;
/**
* Question 4 [20 Points] Sales Chart.
*
* Write a program that processes a daily sales file for a store. The daily
* sales file contains the dollar amount of sales for a day on each line of the
* file. The program should output to a file and display to the console a bar
* chart comparing each day's dollar amount. Create each bar in the bar chart by
* displaying a row of asterisks. Each asterisk should represent $100 of sales.
*
* Here's an example of the program's output: DAILY SALES CHART Day 1: ********
* Day 2: ********** Day 3: ***** Day 4: ******************** Day 5:
* ****************** Day 6: * Day 7: ********** Day 8: ********* Day 9:
* ********** Day 10: **************
*
* The input file is provided at "Question4Files/StoreDailySales.txt". The
* output file should be stored at "Question4Files/SalesReport.txt". Overwrite
* the file if it exists. Tip: Use formatted strings with String.format for the
* output, providing a width of 3 for the day number. That way the bar chart
* will line up for up to 999 days.
*
* Comments and style are worth up to 2 points.
*
* @author Brad Green
*/
public class Question4 {
public static void main(String[] args) throws IOException {
int days = 1;
double amount;
double bar;
String fileName = "Question4Files/StoreDailySales.txt";
File file = new File(fileName);
Scanner dailySales = new Scanner(file);
File outputFile = new File("Question4Files/SalesReport.txt");
PrintWriter salesReport = new PrintWriter(outputFile);
salesReport.println("Daily Sales Chart\n");
while(dailySales.hasNextDouble()) {
amount = dailySales.nextDouble();
amount /= 100;
for(double i = 1; i < amount; i++) {
System.out.printf("Day " + "%3d" + ": \n", days++);
}
}
dailySales.close();
}
}