0

I'm coding a programme that calculates data from an input file and compares the results with user input. I have been given a specific input file that I am not supposed to edit. However, for my code I removed the empty line at the end of the input data in order for my code to work correctly, but apparently I am not able to do this. I am now looking for a way to edit my code to allow 1 blank/empty line at the bottom of the input file after the last line of data, but still have my code work exactly as it currently does. At the moment, my code does not work correctly with the empty/blank line at the bottom.

Here is my code;

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

    }

}

Here is my current input file data with the bottom line removed;

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

Here is what the bottom of input file should and needs to look like;

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

I believe I just need to change something at this point in the code;

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

As the error I am given with the empty line added points to line 128, which is input.next();

Any help is appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Yaz
  • 55
  • 3
  • 10

2 Answers2

0

scanner.hasNext() and line.isEmpty() should help you out. You just want to ignore the empty lines in the file.

Dodd10x
  • 3,344
  • 1
  • 18
  • 27
  • Ahh yes, thank you for the answer. Do I change these in the `if` statement? – Yaz Dec 05 '13 at 15:34
  • Yes, you put these in an if statement and decide if you want to continue executing the loop or move on. There are various ways to do this such os using if/else or a continue statement. – Dodd10x Dec 05 '13 at 16:05
  • At the beginning of your while loop, check to see if the string is empty. If so, use a continue statement. – Dodd10x Dec 05 '13 at 20:49
0

As Dodd10x suggested that is one solution. Another if you wanted to keep your loop 100% in tact would be to change the input file without changing the input file so to speak.

How to do this? Well presumably this is an assignment you are doing and cannot modify the actual file, but you likely can just read the file into a string and then modify the string right?

See here: How do I create a Java string from the contents of a file?

Then just change the string and kill the last 2 characters (which would be the newline "\n"). Substring can help with this.

Then instead of having the scanner on the file, have the scanner on the string as input. Then your code works as you want it to.

Community
  • 1
  • 1
Jordan Johns
  • 745
  • 2
  • 10
  • 20