How do I get a line of text to print to the console from an input file??
Hello! This is the last question I have about this program, then I'll leave you guys alone, I promise. All I need to know is how to print the line of numbers to my console/output file. How do I get the console to just print the numbers for Amarillo in that same order of "3 2 1 3 5" minus the negative number. So for example, I just want to say "The values entered are: " + whatever code I need. The rest of the code is perfect I just need that. Thanks so much!!
Input file:
Code java:
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
Program:
Code java:
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);
System.out.println("Welcome to the Rose State Carbon Footprint Calculator!\n" +
"We are going to calculate average carbon footprints for cities and their corresponding fines.\n");
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);
int cityCount = 0;
double totalFines = 0;
while (Fread.hasNext()) {
String outputData = "";
String cityName = "";
cityName = Fread.next();
int valueCF = 0;
valueCF = Fread.nextInt();
int sum = 0;
int count = 0;
double fine = 0;
cityCount ++;
outputData += "Data for city of: " + cityName + "\n";
outputData += "Carbon footprint values are: " ?????????????????
if (valueCF < 0) {
outputData = "Sorry, " + cityName + " does not have any available data. Please try again at another date.";
}
else{
while (valueCF >= 0) {
sum += valueCF;
count ++;
valueCF = Fread.nextInt();
}
double realAverage = (double) sum/count;
int roundedAverage = (int) Math.round(realAverage);
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 + "\n\n";
totalFines += fine;
}
PS.println(outputData);
}
PS.printf("Total fines for the city are: %.2f\n", totalFines);
PS.println("Total number of cities counted: " + cityCount);
Fread.close();
PS.close();
}
}