my program works correctly in the sense that there are no compiling errors. Once I've been through the data and have entered some test data e.g. Firstname: Bob, Surname: Jones, Course: IT, Grade: 44. Firstly, it stores it in the "Grades.txt" file. However, when I re-run the program, the file resets and it's empty. Why is this? It is as if the data is only being temporarily stored in the file. Thanks
Code:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class ExamGrades {
public static void main(String[] args) throws Exception {
FileWriter grades = new FileWriter("grades.txt");
BufferedWriter bw = new BufferedWriter(grades);
PrintWriter out = new PrintWriter(bw);
Scanner scan = new Scanner(System.in);
int examMark =0;
String firstName = "";
String surName = "";
String course = "";
System.out.print("Please enter student firstname: ");
firstName = scan.next();
System.out.print("Please enter student surname: ");
surName = scan.next();
System.out.print("Please select student course: ");
course = scan.next(); // Drop Down menu in future?
System.out.print("Please enter student mark: ");
while (!scan.hasNextInt())
{
System.out.print("Please enter a valid mark: ");
scan.next();
}
examMark = scan.nextInt();
if (examMark < 40)
{
System.out.println("Failed");
}
else if (examMark >= 40 && examMark <= 49)
{
System.out.println("3rd");
}
else if (examMark >= 50 && examMark <= 59)
{
System.out.println("2/2");
}
else if (examMark >= 60 && examMark <= 69)
{
System.out.println("2/1");
}
else if (examMark >= 70 && examMark <= 100)
{
System.out.println("1st");
}
else
{
System.out.println("Invalid Mark"); // If not within previous criteria, this prints
}
out.print(firstName + " ");
out.print(surName + ", ");
out.print(course + ", ");
out.print(examMark);
out.close();
scan.close();
}
}