简体   繁体   中英

I can't figure out why my program isn't looping

I am trying to make this file loop, but I keep getting an error on at the last if statement (Marked by an arrow).

The program is supposed to read in a file with the first line being the name of the customer.
On the second line, the first number is the number of trees to be removed(150 per tree), the second number is tree trimming to be done(50 an hour).
The third line are all the stumps to be removed and their diameter(one number is one stump and also it's diameter).

This is the file that is supposed to be read in ( http://pastebin.com/gXkujcaM ).

public class Prog_5 {

    public static void main(String[] args) throws FileNotFoundException {
        String name = "Joe";
        double trees = 0;
        double treeTrimming = 0;
        double stumpInches = 0;
        double stumpTotal = 0;
        double total = 0;
        double totalRev = 0;

        Scanner in = new Scanner(System.in);
        System.out.print("Input file: ");
        String inputFile = in.nextLine();

        System.out.print("Output file: ");
        String outputFile = in.nextLine();
        in.close();

        File input = new File(inputFile);
        in = new Scanner(input);
        PrintWriter output = new PrintWriter(outputFile);

        while(in.hasNext()){

        name = in.nextLine();
        output.println("Customer: " + name);
        System.out.println("Customer: " + name);

        trees = in.nextDouble();
        trees *= 150;
        output.println("Tree Removal: $" + trees);
        System.out.println("Tree Removal: $" + trees);

        treeTrimming = in.nextDouble();
        treeTrimming *= 50;
        output.println("Tree Trimming: $" + treeTrimming);
        System.out.println("Tree Trimming: $" + treeTrimming);

        while (in.hasNextDouble()) {
            stumpInches = in.nextDouble();
            if (stumpInches != -1) {
                stumpTotal = stumpTotal + 30;
                if (stumpInches > 12) {
                    stumpInches -= 12;
                    stumpInches *= 2;
                }
                stumpTotal += stumpInches;
            }

        }
        output.println("Stump Removal: $" + stumpTotal);
        System.out.println("Stump Removal: $" + stumpTotal);

        total = (trees + treeTrimming + stumpTotal);
        output.println("Total: $" + total);
        System.out.println("Total: $" + total);

        totalRev += total;
        stumpTotal = 0;
        trees = 0;
        treeTrimming = 0;

        if(in.hasNext());
            in.next();
        }

        output.close();

    }

}

it is your if loop is problem . you should write it like below . but you terminated it with ;

if(in.hasNext()){
       in.next();
        }

so it will keep throwing nosuchelement exception every time if it reaches EOF

There is no need of outer while loop, if condition at the end. Inner while loops takes care of what your program is intended to do. PFB corrected code:

import java.io.*;
import java.util.Scanner;

public class Prog_5 {

    public static void main(String[] args) throws FileNotFoundException {
        String name = "Joe";
        double trees = 0;
        double treeTrimming = 0;
        double stumpInches = 0;
        double stumpTotal = 0;
        double total = 0;
        double totalRev = 0;

        Scanner in = new Scanner(System.in);
        System.out.print("Input file: ");
        String inputFile = in.nextLine();

        System.out.print("Output file: ");
        String outputFile = in.nextLine();
        in.close();

        File input = new File(inputFile);
        in = new Scanner(input);
        PrintWriter output = new PrintWriter(outputFile);

        // while (in.hasNext()) {

        name = in.nextLine();
        output.println("Customer: " + name);
        System.out.println("Customer: " + name);

        trees = in.nextDouble();
        trees *= 150;
        output.println("Tree Removal: $" + trees);
        System.out.println("Tree Removal: $" + trees);

        treeTrimming = in.nextDouble();
        treeTrimming *= 50;
        output.println("Tree Trimming: $" + treeTrimming);
        System.out.println("Tree Trimming: $" + treeTrimming);

        while (in.hasNextDouble()) {
            stumpInches = in.nextDouble();
            if (stumpInches != -1) {
                stumpTotal = stumpTotal + 30;
                if (stumpInches > 12) {
                    stumpInches -= 12;
                    stumpInches *= 2;
                }
                stumpTotal += stumpInches;
            }

        }
        output.println("Stump Removal: $" + stumpTotal);
        System.out.println("Stump Removal: $" + stumpTotal);

        total = (trees + treeTrimming + stumpTotal);
        output.println("Total: $" + total);
        System.out.println("Total: $" + total);

        totalRev += total;
        stumpTotal = 0;
        trees = 0;
        treeTrimming = 0;

        // if (in.hasNext())
        // ;
        // in.next();
        // }
        in.close();
        output.close();

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM