Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Urgently need help with counting total of cities/fines for an output file!!

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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:
    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:
    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();
     
    	}
     
    }
    Last edited by Eclecstatic; October 29th, 2012 at 12:01 AM.


Similar Threads

  1. Counting a total and printing value from 2 arrays
    By stewart86 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 2nd, 2012, 07:25 AM
  2. counting the values in input file and and writing the output to a file
    By srujirao in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 8th, 2012, 02:48 PM
  3. Counting Words in a File with a Loop
    By bengregg in forum Loops & Control Statements
    Replies: 17
    Last Post: February 11th, 2011, 10:11 AM
  4. counting words of a text file
    By maybach230 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 6th, 2010, 03:40 PM
  5. Reading from a text file. Help needed urgently.
    By TheAirPump in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 14th, 2009, 06:16 PM