Hello. (I posted this yesterday night, but I think the whole "new server" thing messed with it, so sorry if you've seen this already.) I'm a little new to all of this, so forgive me if answer is super obvious. My assignment is to create a program with a Nested Loop and a Nested If/Else. The program is to calculate what fines certain cities would pay based on their carbon footprint. Our professor gave us certain input files to use as well, which is listed below. I feel like I ALMOST have it, but I'm just missing one or two things. I put what I have right now, but here's the problem, the outer loop just keeps going on and on, flooding the output file. I don't know how to get it to close. Or maybe I have some of the code out of order. I'm not sure. Please explain what's going on, because I've been working so hard on this assignment and just want to know the (probably) very simple issue with it. Thanks so much in advance!
Here's what the input file has in it. It's just in a basic .txt file.
Amarillo 3 2 1 3 5 -7 Rochester 5 6 7 4 6 -2 Albuquerque 3 4 5 2 3 -9 Durham 9 8 7 9 9 8 -4 Boise 3 4 6 2 8 9 -1 Jacksonville 2 5 2 1 5 7 2 1 1 -3 Lexington 8 9 12 3 4 10 11 -6 Tulsa 8 2 9 5 6 11 8 4 2 9 -1 San_Francisco 1 1 2 1 1 -8 Washington -3
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintStream; import java.util.Scanner; public class CFPInputOutput { /** * @param args * @throws */ public static void main(String[] args) throws FileNotFoundException { Scanner QWERTY = new Scanner(System.in); String inputFileName = ""; System.out.println("Enter path to input file"); inputFileName = QWERTY.nextLine(); File Fin = new File(inputFileName); Scanner Fread = new Scanner(Fin); System.out.println("Enter path to output file"); String outFileName = ""; outFileName = QWERTY.nextLine(); File Fout = new File(outFileName); PrintStream PS = new PrintStream(Fout); String outputData = ""; String cityName = ""; cityName = Fread.next(); int valueCF = 0; valueCF = Fread.nextInt(); while (Fread.hasNext()) { int sum = 0; int count = 0; while (valueCF >=0) { sum += valueCF; count ++; valueCF = Fread.nextInt(); } double realAverage = (double) sum/count; int roundedAverage = (int) Math.floor(realAverage); double fine = 0; if (roundedAverage<=1) { fine = 0.0; } else if (roundedAverage>1 && roundedAverage<=3) { fine = 1000000; } else if (roundedAverage>3 && roundedAverage<=5) { fine = 2000000; } else if (roundedAverage>5 && roundedAverage<=7) { fine = 3000000; } else if (roundedAverage>7) { fine = 4500000; } outputData += "The sum of all Carbon FootPrint values is: " + sum + "\n"; outputData += "The total number of readings is: " + count + "\n"; outputData += "The real average carbon footprint is: " + realAverage + "\n"; outputData += "To benefit you, the rounded down number is: " + roundedAverage + "\n"; outputData += "The fine for " + cityName + " is $" + fine; PS.println(outputData); } Fread.close(); PS.close(); } }
Thanks again!