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 2 of 2

Thread: Big problems with my hasNext method and outer loop! Please help!

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

    Question Big problems with my hasNext method and outer loop! Please help!

    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!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,988
    My Mood
    Hungover
    Thanks
    133
    Thanked 557 Times in 478 Posts

    Default Re: Big problems with my hasNext method and outer loop! Please help!

    You never read in the endline character after the negative number.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. For loop; Problems with my for loop
    By mingleth in forum Loops & Control Statements
    Replies: 5
    Last Post: November 16th, 2011, 06:24 PM
  2. Replies: 2
    Last Post: July 7th, 2011, 03:46 PM
  3. [SOLVED] Problems with Loop Structure
    By Sean137 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 13th, 2010, 01:59 PM
  4. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  5. big problems bounding() in Java2d with LineBreakMeasurer.
    By fenderman in forum AWT / Java Swing
    Replies: 0
    Last Post: July 27th, 2009, 01:15 PM