Reputation: 21
The program I have below works perfectly when I print it out in the Java console but when I try to append the program to be put into a text file, it only prints 1/5 of the student's averages into the appending text file.
Bobby, average = 93
I want it to be printing all 5 student's averages as so
Agnes, average = 76
Bufford, average = 91
Julie, average = 94
Alice, average = 39
Bobby, average = 93
Thanks in advance.
import java.util.*;
import java.io.*;
public class StudentAverage {
public static void main(String[]args) throws IOException {
Scanner scanner = new Scanner(new
File("D:\\School\\StudentGrades.txt"));
while (scanner.hasNextLine()) {
Scanner scanners = new Scanner(scanner.nextLine());
String name = scanners.next();
double total = 0;
int num = 0;
while (scanners.hasNextInt()) {
total += scanners.nextInt();
num++;
}
PrintStream output = new PrintStream (("D:\\School\\WriteStudentAverages.txt"));
output.print(name + ", average = " + (Math.round(total/num)));
output.flush();
}
scanner.close();
}
}
Upvotes: 0
Views: 762
Reputation: 1017
There are a few more glitches in your code. Since you need to manage both the Scanner as well as the PrintWriter with a try/resource-construct, I'd prefer to keep the input- and output-routines separate and read the relevant contents of your file temporarily into memory.
Here is an idea:
LinkedHashMap<String, Integer> students = new LinkedHashMap<>();
// Input routine
try (Scanner scanner = new Scanner(new File("D:\\School\\StudentGrades.txt"))) {
while (scanner.hasNextLine()) {
try (Scanner scanners = new Scanner(scanner.nextLine())) {
String name = scanners.next();
int total = 0;
int num = 0;
while (scanners.hasNextInt()) {
total += scanners.nextInt();
num++;
}
students.put(name, (int) Math.round((double)total/num));
}
}
}
catch (FileNotFoundException e) {
// do something smart
}
// Output routine
try (PrintStream output = new PrintStream ("D:\\School\\WriteStudentAverages.txt")) {
for (Entry<String, Integer> student : students.entrySet()) {
output.println(student.getKey() + ", average = " + Integer.toString(student.getValue()));
}
}
catch (FileNotFoundException e) {
// do something smart
}
The above also allows you to get rid of that nasty throws IOException
in the signature of your main()
-method. Instead, there are now two beautiful skeletons of an exception handler (the two catch blocks) where you can put some logic that triggers in case
Upvotes: 0
Reputation: 2707
To append your output/text file you have to use a different way of writing it.
I also suggest using try-with-resource blocks to avoid memeory leaks.
try (OutputStream output = new FileOutputStream("myFile.txt", true);
Writer writer = new OutputStreamWriter(output)) {
writer.write(name + ", average = " + (Math.round(total / num)) + '\n');
}
You don't have to flush/close them manually
Upvotes: 1
Reputation: 362
PrintStream output = new PrintStream (("D:\\School\\WriteStudentAverages.txt"));
every time it gets to this line, it deletes the file ,opens a new file and adds only the current line. write this line before the loop, and in the loop just leave your other code as is.
Upvotes: 3