d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Why Is This Iterating 96 Times?
Add Reply New Topic New Poll
Member
Posts: 2,183
Joined: May 8 2009
Gold: 1.00
Mar 10 2014 09:47am
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();

}

}
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 10 2014 10:17am
Divide each number in the input file by 100 and add them up. I bet you get 96.
Member
Posts: 5,988
Joined: May 6 2006
Gold: 30.00
Mar 10 2014 11:29am
I'm guessing the System.out statement should be right before the for loop, and the for loop should contain System.out.print("*") - and it looks like that would yield the correct output
Member
Posts: 2,183
Joined: May 8 2009
Gold: 1.00
Mar 10 2014 09:35pm
I figured it out, didn't need 3 loops.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll