0

So we want an application to allow the user to enter the names and grades of students the user should be prompted for the name of the file to create and for the number of students to be entered (1 grade per student). Then the program takes all of the grades and averages them. The problem is that it is not reading the file and always gives us a average of -0.0.

`

public static void main(String[] args) throws IOException {

    System.out.println("What is the name of the file you would like to create?");
    filename = p.next();

    File fd = new File(filename + ".txt");
    fd.createNewFile();
    students(fd);
}

public static void students(File fd) throws IOException {
    int numbstudents;
    FileWriter ap = new FileWriter(fd, true);
    BufferedWriter ad = new BufferedWriter(ap);

    System.out.println("How many students would you like to add?");
    numbstudents = p.nextInt();
    int i = 0;
    while (i != numbstudents) {
        for (i = 0; i < numbstudents; i++) {
            System.out.println("What is the name of student number " + i + " ?");
            String name = p.next();
            ad.write(name);
            ad.newLine();
            System.out.println("What grade did student number " + i + " acheive?");
            String a = f.next();
            ad.write(a);
            ad.newLine();

        }
    }

    read(fd);
    ad.close();
}

public static void read(File fd) throws FileNotFoundException {

    int counter = 0;
    FileReader h;
    BufferedReader g;
    String test;
    double average, total = 0;
    int number = 0;
    int i = 0;
    try {
        h = new FileReader(fd);
        g = new BufferedReader(h);
        while ((test = g.readLine()) != null) {
            number += 1;
            System.out.println(test);
            counter = counter + 1;
            i = counter % 2;
            if (i == 0) {
                total += Double.parseDouble(test);
            }

        }
        average = total / (number - 1);
        System.out.println("The students average is: " + average);

        g.close();
        fd.delete();
    } catch (FileNotFoundException e) {
        System.out.println("File could not be found.");
    } catch (IOException e) {
        System.out.println("Your file could not be read.");
    }

}

} `

1 Answers1

3

You're attempting to read from the file before you've closed the writer.

The close() call includes flushing buffered data to disk. You're reading before data is flushed to disk.

As a side note, consider what you're accomplishing with this pair of statements:

while (i != numbstudents) {
    for (i = 0; i < numbstudents; i++) {

The while is unnecessary. The for statement iterates over the comfortably numb students.

Also note the difference in conditions between the two. In general, when iterating over numbers, it's safer to use '<', '<=', '>' or '>=' than '==' or '!='. Otherwise, if you pass by the endpoint before an equality condition, it will continue happily past the end.

Finally, consider naming your methods with descriptive verb phrases. This will help you with breaking the big problem down into smaller pieces. For example, you could have one method called inputStudents() that reads input and creates and closes the file, called before another method printAverageOfStudents() that reads the file and computes the average.

Andy Thomas
  • 78,842
  • 10
  • 93
  • 142