更改代码以允许输入文件中的额外行

时间:2013-12-06 14:14:06

标签: java input

我正在寻找一种方法来更改我的代码以允许输入文件底部的空白/空行。我的代码完美无需底部的空行,但是,我需要在数据文件的末尾添加一个额外的空行。是否有人能够更改我的代码以允许在我的输入文件底部添加一个额外的空行。非常感谢。

我当前的代码;

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 added and will be empty

如果我现在添加额外的行代码,我会收到此错误;

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Assignment3.main(Assignment3.java:123)

第123行是;

if (input.hasNextLine()) { 
input.nextLine();
input.next(); //Line 123

1 个答案:

答案 0 :(得分:1)

试试这个:

if (input.hasNextLine()) { //If statement to check for next input
    try {
        input.nextLine();
        input.next();
    } catch (NoSuchElementException e) {
        break;
    }
}