我无法弄清楚为什么我的程序没有循环

时间:2015-06-29 20:03:11

标签: java if-statement while-loop

我正在尝试使这个文件循环,但是我在最后一个if语句中继续收到错误(用箭头标记)。

程序应该读入一个文件,第一行是客户的名字 在第二行,第一个数字是要删除的树的数量(每棵树150个),第二个数字是要完成的树修剪(每小时50个)。
第三行是要去除的所有树桩和它们的直径(一个数字是一个树桩,也是它的直径)。

这是应该读入的文件(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();

    }

}

2 个答案:

答案 0 :(得分:0)

这是你的if循环问题。你应该写如下。但你终止了它;

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

所以如果它达到EOF,它将继续抛出nosuchelement异常

答案 1 :(得分:0)

如果条件结束,则不需要外部while循环。内部while循环负责您的程序要执行的操作。 PFB更正的代码:

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();

    }
}