我在“private reader = new BufferedReader(new FileReader(deleteRecord));”上找到以下消息;“:
令牌“reader”上的语法错误,此令牌后面的VariableDeclartorID
代码:
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 ExamGradesMethods {
private static int examMark = 0;
private static int menu = 0;
private static String firstName = "";
private static String firstNameDelete = "";
private static String lastName = "";
private static String lastNameDelete = "";
private static String unit = "";
private static String entry = "";
private static String firstCap = "";
private static String surCap = "";
private static Scanner scan = new Scanner(System.in);
private static BufferedReader reader = null;
private FileWriter grades = new FileWriter("GradeEnter.txt",true);
private BufferedWriter bw = new BufferedWriter(grades);
private PrintWriter out = new PrintWriter(bw);
private reader = new BufferedReader(new FileReader(deleteRecord));
private static File deleteRecord = new File("GradeEnter.txt");
public static void menuActions()
{
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:
enterGrades();
break;
case 2:
viewGrades();
break;
case 3:
deleteGrades();
break;
case 4:
exitProgram();
break;
default:
menuActions();
}
}
public static void enterGrades()
{
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: ");
lastName = scan.next();
while(!lastName.matches("[-a-zA-Z]*"))
{
System.out.print("Please enter a valid surname: ");
lastName = scan.next();
}
surCap = lastName.substring(0,1).toUpperCase() + lastName.substring(1);
System.out.print("Please select Subject Unit: ");
unit = 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 + ", " + unit + ", " + examMark);
// out.println(entry);
menuActions();
}
public static void viewGrades() {
int i =1;
String line;
while ((line = reader.readLine()) != null) {
System.out.println(i + ") " + line);
i++;
}
menuActions();
}
public static void deleteGrades(){
int i = 1;
String line;
File tempFile = new File("MyTempFile.txt");
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
System.out.println("Current Entries Stored: ");
i =1;
while ((line = reader.readLine()) != null) {
System.out.println(i + ") " + line);
i++;
}
Scanner scanner = new Scanner(System.in);
System.out.print("To delete, please enter student's First Name: ");
firstNameDelete = scanner.nextLine();
System.out.print("Now, please enter student's Surname: ");
lastNameDelete = scanner.nextLine();
reader.close();
reader = new BufferedReader(new FileReader(deleteRecord));
String currentLine = reader.readLine();
while(currentLine != null) {
if(!currentLine.contains(firstNameDelete) && !currentLine.contains(lastNameDelete)) {
writer.write(currentLine);
writer.newLine();
}
currentLine = reader.readLine();
}
System.out.print("if name matches, it will be deleted ");
reader.close();
writer.close();
deleteRecord.delete();
tempFile.renameTo(deleteRecord);
}
public static void exitProgram(){
System.out.println("Thanks for using 'GradeEnter' ");
System.exit(0);
// Are you sure you want to exit gui?
}
public static void main(String[] args) throws Exception {
System.out.println("Welcome to the 'GradeEnter' program! ");
menuActions();
}
}
我环顾四周,无法找到关于这个问题的任何事情。如果有人可以提供帮助,我将不胜感激。感谢
答案 0 :(得分:2)
reader
需要声明中的类型,变量deleteRecord
在声明之前也会被使用 - 但是定义reader
的语句会抛出一个已检查的异常,因此需要放置在代码块而不是类块
public class ExamGradesMethods {
...
private void readRecords() throws FileNotFoundException {
File deleteRecord = new File("GradeEnter.txt");
BufferedReader reader = new BufferedReader(new FileReader(deleteRecord));
...
}
}
阅读:Variables