2015-04-25 56 views
0

我得到就行了以下信息:「私人的讀者=新的BufferedReader(新的FileReader(deleteRecord));」:令牌「讀者」誤差VariableDeclartorID後預計此令牌

語法錯誤,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

Java是一種面向對象的語言 - 讓所有'static'變量實例變量 – Reimeus

回答

2

reader需要聲明一個類型,變量deleteRecord也被用來宣告前它 - 但是它定義reader聲明拋出一個checked異常,以便將需要被放置在一個代碼塊,而不是類塊

public class ExamGradesMethods { 

    ... 
    private void readRecords() throws FileNotFoundException { 
     File deleteRecord = new File("GradeEnter.txt"); 
     BufferedReader reader = new BufferedReader(new FileReader(deleteRecord)); 
     ... 
    } 
} 

閱讀:Variables

+0

你能在此解釋一下? – Adam

+0

查看更新..... – Reimeus

+0

我已經更新了上面顯示的所有代碼,並且努力實現你在我的 – Adam

相關問題