更改代码以允许输入文件底部的空行

时间:2013-12-05 14:36:56

标签: java input

我编写了一个程序,用于计算输入文件中的数据并将结果与​​用户输入进行比较。我收到了一个我不应该编辑的特定输入文件。但是,对于我的代码,我删除了输入数据末尾的空行,以便我的代码正常工作,但显然我无法做到这一点。我现在正在寻找一种方法来编辑我的代码,以便在最后一行数据之后在输入文件的底部允许1个空白/空行,但仍然让我的代码完全按照当前的方式工作。目前,我的代码无法正常使用底部的空/空行。

这是我的代码;

//Packages are imported
import java.io.*;
import java.lang.*;
import java.util.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Assignment3 {

static Scanner console = new Scanner(System.in); //New scanner is created

public static void main(String[] args) throws FileNotFoundException { //FileNotFoundException is thrown

    Scanner input = new Scanner(
            new FileReader("AssistantHoursAndRates.txt")); //New input scanner is created to read file 'AssistantHoursAndRates.txt

    double UnitRM1; // Double variables are created to store the recommended maximum staff costs for each unit. Here is Unit1's recommended staff cost
    System.out.println("Enter recommended maximum staff cost of Unit 1");
    UnitRM1 = console.nextDouble(); //Unit1's recommended staff cost is equal to the next console input by the user
    System.out.println("Recommended maximum staff cost of Unit1 = "+ UnitRM1); //Unit1's recommended staff cost is printed into console


    System.out.printf("%10s\n", " "); //Line used to print space in console for easier readability

    double UnitRM2; //Unit 2's recommended staff cost is stored here
    System.out.println("Enter recommended maximum staff cost of Unit 2");
    UnitRM2 = console.nextDouble();
    System.out.println("Recommended maximum staff cost of Unit2 = "+ UnitRM2);


    System.out.printf("%10s\n", " ");

    double UnitRM3; //Unit 3's recommended staff cost is stored here
    System.out.println("Enter recommended maximum staff cost of Unit 3");
    UnitRM3 = console.nextDouble();
    System.out.println("Recommended maximum staff cost of Unit3 = "+ UnitRM3);


    System.out.printf("%10s\n", " ");

    double UnitRM4; //Unit 4's recommended staff cost is stored here
    System.out.println("Enter recommended maximum staff cost of Unit 4");
    UnitRM4 = console.nextDouble();
    System.out.println("Recommended maximum staff cost of Unit4 = "+ UnitRM4);


    System.out.printf("%10s\n", " ");

    double UnitRM5; //Unit 5's recommended staff cost is stored here
    System.out.println("Enter recommended maximum staff cost of Unit 5");
    UnitRM5 = console.nextDouble();
    System.out.println("Recommended maximum staff cost of Unit5 = "+ UnitRM5);


    System.out.printf("%10s\n", " ");

    double UnitRM6; //Unit 6's recommended staff cost is stored here
    System.out.println("Enter recommended maximum staff cost of Unit 6");
    UnitRM6 = console.nextDouble();
    System.out.println("Recommended maximum staff cost of Unit6 = "+ UnitRM6);


    System.out.printf("%10s\n", " ");

    double UnitRM7; //Unit 7's recommended staff cost is stored here
    System.out.println("Enter recommended maximum staff cost of Unit 7");
    UnitRM7 = console.nextDouble();
    System.out.println("Recommended maximum staff cost of Unit7 = "+ UnitRM7);


    System.out.printf("%10s\n", " ");

    double UnitRM8; //Unit 8's recommended staff cost is stored here
    System.out.println("Enter recommended maximum staff cost of Unit 8");
    UnitRM8 = console.nextDouble();
    System.out.println("Recommended maximum staff cost of Unit8 = "+ UnitRM8);


    System.out.printf("%10s\n", " ");

    double UnitRM9; //Unit 9's recommended staff cost is stored here
    System.out.println("Enter recommended maximum staff cost of Unit 9");
    UnitRM9 = console.nextDouble();
    System.out.println("Recommended maximum staff cost of Unit9 = "+ UnitRM9);


    double[] totals = new double[9]; //An array is created to store totals from calculation throughout the while loop
    int unit = 1;
    while (input.hasNextLine()) { //A while loop is created to run through the input file, calculating the values and storing the totals in the array.
        String line = input.nextLine();
        System.out.println(line);

        double total = 0;

        int assistants = input.nextInt();
        System.out.println("Number of Assistants " + assistants); //Lines are printed to console to display calculations and results
        System.out.println("Hours  Rate");
        System.out.println("------------");
        for (int i = 0; i < assistants; i++) { //for is created to read integers and doubles and calculate results
            int hours = input.nextInt();
            System.out.print(hours + "     ");
            double rate = input.nextDouble();
            System.out.println(rate);
            total += (hours * rate);
        }

        System.out.println("Total cost of Unit " + unit + " is " + total); //At the end of each loop, the total cost of each unit is printed and stored in the array
        System.out.println();
        totals[unit - 1] = total; //Array totals are stored here
        unit++;

        if (input.hasNextLine()) { //If statement to check for next input
            input.nextLine();
            input.next();

        }

    }

    System.out.println("Comparisons are as follows;"); //Console print out to display comparison results

    String fileName = "results.txt"; //File name is created for output file
    try {
        PrintWriter outputStream = new PrintWriter(fileName); //New PrintWriter is created for fileName if file is not already found

        if (UnitRM1 < totals[0]) { //The following if and else statements compare the user input RM to the totals stored in the array.
            outputStream.println("Unit 1 = " +totals[0]); //If user RM is less than array index total, total is printed to output file 'results.txt'
            System.out.println("Unit 1 total staff cost is less than recommended maximum staff cost!"); //Console print is also printed for all results regardless


        }

        else //Else statement for if Unit total is more than RM
            System.out.println("Unit 1 total staff cost is more than recommended maximum staff cost!");


        if (UnitRM2 < totals[1]) { //Unit2 RM comparison to Unit 2 total stored in array
            outputStream.println("Unit 2 = " +totals[1]);
            System.out.println("Unit 2 total staff cost is less than recommended maximum staff cost!");


        }

        else
            System.out.println("Unit 2 total staff cost is more than recommended maximum staff cost!");


        if (UnitRM3 < totals[2]) { //Unit3 RM comparison to Unit 3 total stored in array
            outputStream.println("Unit 3 = " +totals[2]);
            System.out.println("Unit 3 total staff cost is less than recommended maximum staff cost!");


        }

        else
            System.out.println("Unit 3 total staff cost is more than recommended maximum staff cost!");


        if (UnitRM4 < totals[3]) { //Unit4 RM comparison to Unit 4 total stored in array
            outputStream.println("Unit 4 = " +totals[3]);
            System.out.println("Unit 4 total staff cost is less than recommended maximum staff cost!");


        }

        else
            System.out.println("Unit 4 total staff cost is more than recommended maximum staff cost!");


        if (UnitRM5 < totals[4]) { //Unit5 RM comparison to Unit 5 total stored in array
            outputStream.println("Unit 5 = " +totals[4]);
            System.out.println("Unit 5 total staff cost is less than recommended maximum staff cost!");


        }

        else
            System.out.println("Unit 5 total staff cost is more than recommended maximum staff cost!");


        if (UnitRM6 < totals[5]) { //Unit6 RM comparison to Unit 6 total stored in array
            outputStream.println("Unit 6 = " +totals[5]);
            System.out.println("Unit 6 total staff cost is less than recommended maximum staff cost!");


        }

        else
            System.out.println("Unit 6 total staff cost is more than recommended maximum staff cost!");


        if (UnitRM7 < totals[6]) { //Unit7 RM comparison to Unit 7 total stored in array
            outputStream.println("Unit 7 = " +totals[6]);
            System.out.println("Unit 7 total staff cost is less than recommended maximum staff cost!");


        }

        else
            System.out.println("Unit 7 total staff cost is more than recommended maximum staff cost!");


        if (UnitRM8 < totals[7]) { //Unit8 RM comparison to Unit 8 total stored in array
            outputStream.println("Unit 8 = " +totals[7]);
            System.out.println("Unit 8 total staff cost is less than recommended maximum staff cost!");


        }

        else
            System.out.println("Unit 8 total staff cost is more than recommended maximum staff cost!");


        if (UnitRM9 < totals[8]) { //Unit9 RM comparison to Unit 9 total stored in array
            outputStream.println("Unit 9 = " +totals[8]);
            System.out.println("Unit 9 total staff cost is less than recommended maximum staff cost!");


        }

        else
            System.out.println("Unit 9 total staff cost is more than recommended maximum staff cost!");


        outputStream.close(); //Stream is closed

    } catch (FileNotFoundException e) { //Catch FileNotFoundException in relation with try statement
        e.printStackTrace();
    }

    }

}

以下是我当前的输入文件数据,删除了底线;

Unit One
4
32 8
38 6
38 6
16 7

Unit Two
0

Unit Three
2
36 7
36 7

Unit Four
6
32 6.5
32 6.5
36 6.5
36 6.5
38 6.5
38 6.5

Unit Five
4
32 6.5
32 8
32 7
32 8

Unit Six
5
38 7
30 6.5
24 8
24 8
24 8

Unit Seven
0

Unit Eight
1
40 12

Unit Nine
5
24 8
24 6.5
30 6.5
24 7
32 7

以下是输入文件的底部应该和需要的样子;

Unit Nine
5
24 8
24 6.5
30 6.5
24 7
32 7
//This line will be empty

我相信我只需要在代码中改变一些内容;

if (input.hasNextLine()) { //If statement to check for next input
input.nextLine();
input.next();

由于我给出的空行错误添加了指向第128行的点,即input.next();

感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

scanner.hasNext()和line.isEmpty()可以帮助你。你只想忽略文件中的空行。

答案 1 :(得分:0)

正如Dodd10x所说,这是一个解决方案。另一个如果你想保持你的循环100%,那就是更改输入文件而不改变输入文件。

怎么做?好吧,这可能是你正在做的一项任务,无法修改实际文件,但你可能只是将文件读入一个字符串,然后修改字符串吧?

见这里:How do I create a Java string from the contents of a file?

然后只需更改字符串并删除最后2个字符(这将是换行符“\ n”)。子串可以帮助解决这个问题。

然后,不要将扫描仪放在文件上,而是将扫描仪放在字符串上作为输入。然后你的代码就像你想要的那样工作。