if I choose option 3 on this program, I would like it to delete one of the exam grades.
The program outputs like this at the moment if option 2 (view grades) is chosen:
1) Bob Jones, Comp, 18
2) Matt Jones, computing, 100
3) Adam Frank-Jones, Drama, 69
As you can see in the code, the numbers incrementing down the side are represented by "i". What I'm looking for is if I enter "1" for example when choosing a row to delete, it deletes a row (in this case "1) Bob Jones, Comp, 18"). A row is made up of i + First Name + Surname + Course + Mark, all concatenated together.
Now, I have looked around the internet on code helping me do delete an entry. I have tried if i equals user input, then delete. I have tried using a temporary file where the user searches by first name (I am unsure how to code this though") and then writes back to original using BufferedWriter etc. I have been unsuccessful on making this work though.
Can anyone help me? Thanks
Code:
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class ExamGrades {
public static void main(String[] args) throws Exception {
BufferedReader reader = null;
FileWriter grades = new FileWriter("grades2.txt",true);
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 = "";
String entry = "";
String firstCap = "";
String surCap = "";
int menu =0;
System.out.println("Welcome to the 'GradeEnter' program! ");
System.out.println("Menu: ");
System.out.println("1) Enter Student Grade(s)");
System.out.println("2) View Student Grade(s)");
System.out.println("3) Delete Grade(s)");
System.out.println("4) Exit");
menu = scan.nextInt();
switch (menu) {
case 1:
System.out.print("Please enter student first name: ");
firstName = scan.next();
while(!firstName.matches("[-a-zA-Z]*"))
{
System.out.print("Please enter a valid first name: ");
firstName = scan.next();
}
firstCap = firstName.substring(0,1).toUpperCase() + firstName.substring(1);
System.out.print("Please enter student surname: ");
surName = scan.next();
while(!surName.matches("[-a-zA-Z]*"))
{
System.out.print("Please enter a valid surname: ");
surName = scan.next();
}
surCap = surName.substring(0,1).toUpperCase() + surName.substring(1);
System.out.print("Please select student course: ");
course = scan.next();
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");
}
entry = (firstCap + " " + surCap + ", " + course + ", " + examMark);
out.println(entry);
break;
case 2:
File file = new File("grades2.txt");
reader = new BufferedReader(new FileReader(file));
int i =1;
String line;
while ((line = reader.readLine()) != null) {
System.out.println(i + ") " + line);
i++;
}
break;
case 3:
// HERE
break;
case 4:
System.out.println("Thanks for using 'GradeEnter' ");
System.exit(0);
break;
}
out.close();
scan.close();
}
}