2012-08-14 63 views
0

嘿,我的RandomAccessFile應用程序對我的deleteByName方法來說效果不好。基本上我想要這種方法搜索商店的學生,然後從RandomAccessFile中刪除它。搜索位不成問題,但RandomAccessFile仍然顯示員工的詳細信息爲空,而不是完全刪除學生。誰能幫忙?DeleteByName方法不會從randomAccessFile中刪除

這裏是我的代碼:

//--------------------------------------------------------------------------------------- 
//   Name:  Case 3: Delete by Name. 
//   Description: Choice 3 gives the user an option to delete an employee by name. 
//--------------------------------------------------------------------------------------- 
        case 3: 
         System.out.println("Delete by Name."); 
         Student studentDelete = MenuMethods.userInputByName(); 
         details.searchByName(studentDelete.getStudentName()); 
         details.remove(studentDelete.getStudentName()); 
         file.write(studentDelete.toString().getBytes()); 
         break; 

StudentStore

// --------------------------------------------------------------------------------------- 
    // Name: Search by Name. 
    // --------------------------------------------------------------------------------------- 
    public Student searchByName(String studentName) 
    { 
     Student student = map.get(studentName); 
     System.out.println(student); 
     return student; 
    } 
public void write(RandomAccessFile file) throws IOException 
     { 
      for (Student student : map.values()) 
      { 
       byte[] bytes = student.toString().getBytes(); 
       for(byte byteWrite : bytes) 
       { 
        file.writeByte(byteWrite); 
       } 
      } 

     } 

     public void readAll(RandomAccessFile file) throws IOException 
     { 
      /*final int Record_Length = 30; 
      int recordNumber = 0; 
      file.seek((recordNumber) * Record_Length); 

      String code =""; 
      for(int i = 0; i < 30; i++) 
      { 
      code += file.readLine() + "\n"; 
      } 
      System.out.println(code);*/ 
      file.seek(0); 

      String code; 
      while((code = file.readLine())!=null) 
      System.out.println(code); 

     } 

    // --------------------------------------------------------------------------------------- 
    // Name: Remove. 
    // --------------------------------------------------------------------------------------- 
    public Student remove(String key) 
    { 
     // Remove the Employee by name. 
     if (map.containsKey(key)) 
      return map.remove(key); // if it is there remove and return 
     else 
      return null; // if its not there return nothing. 
    } 

MenuMethods

//--------------------------------------------------------------------------------------- 
// Name:   Imports. 
// Description: To allow the use of different Java classes. 
//--------------------------------------------------------------------------------------- 
import java.util.Scanner; 
//--------------------------------------------------------------------------------------- 

public class MenuMethods 
{ 
    private static Scanner keyboard = new Scanner(System.in); 
//--------------------------------------------------------------------------------------- 
// Methods for the Company Application menu. 
//--------------------------------------------------------------------------------------- 

//--------------------------------------------------------------------------------------- 
// Name:   getMenuChoice. 
// Description: Method for validating the choice. 
//--------------------------------------------------------------------------------------- 
    public static int getMenuChoice(String menuString, int limit,String prompt, String errorMessage) 
    { 
     System.out.println(menuString); 
     int choice = inputAndValidateInt(1, limit, prompt, errorMessage); 
     return choice; 
    } 

//--------------------------------------------------------------------------------------- 
// Name:  inputAndValidateInt. 
// Description: This method is used in the getMenuChoice method. 
//--------------------------------------------------------------------------------------- 
    public static int inputAndValidateInt(int min, int max, String prompt,String errorMessage) 
    { 
     int number; 
     boolean valid; 
     do 
     { 
      System.out.print(prompt); 
      number = keyboard.nextInt(); 
      valid = number <= max && number >= min; 
      if (!valid) 
      { 
       System.out.println(errorMessage); 
      } 
     } while (!valid); 
     return number; 
    } 

//--------------------------------------------------------------------------------------- 
// Name:  userInput 
// Description: This method is used in the MainApp to give the user capability to enter 
//    the details when adding details of an employee into the store. 
//--------------------------------------------------------------------------------------- 
    public static Student userInput() 
    { 
     String temp = keyboard.nextLine(); 
     Student s = null; 
     System.out.println("Please enter the Student Name:"); 
     String studentName = keyboard.nextLine(); 
     System.out.println("Please enter the Student ID:"); 
     String studentId = keyboard.nextLine(); 
     System.out.println("Please enter the Student E-mail address:"); 
     String studentEmail = keyboard.nextLine(); 
     System.out.println("Please enter the Student telephone number:"); 
     String studentTelephoneNumber = keyboard.nextLine(); 
     return s = new Student(studentName, studentId, studentEmail,studentTelephoneNumber); 

    } 

//--------------------------------------------------------------------------------------- 
// Name:  userInputByName. 
// Description: This method is used in the MainApp to give the user capability to search by name. 
//--------------------------------------------------------------------------------------- 
    public static Student userInputByName() 
    { 
     // String temp is for some reason needed. If it is not included 
     // The code will not execute properly. 
     String temp = keyboard.nextLine(); 
     Student s = null; 
     System.out.println("Please enter the Student Name:"); 
     String studentName = keyboard.nextLine(); 

     return s = new Student(studentName); 

    } 

//--------------------------------------------------------------------------------------- 
// Name:  userInputByEmail 
// Description: This method is used in the MainApp to give the user capability to search by email. 
//--------------------------------------------------------------------------------------- 
    public static String userInputByEmail() 
    { 
     // String temp is for some reason needed. If it is not included 
     // The code will not execute properly. 
     String temp = keyboard.nextLine(); 
     Student s = null; 
     System.out.println("Please enter the StudentEmail:"); 
     String studentEmail = keyboard.nextLine(); 
     // This can use the employeeName's constructor because java accepts the 
     // parameters instead 
     // of the name's. 
     return studentEmail; 

    } 
//--------------------------------------------------------------------------------------- 
} 

該店是由使用HashMap中。

+1

您的標題不可能更令人困惑。方法不在文件中,因此不能從中刪除。 'DeleteByName()'只是你的代碼的一部分的名稱,它在標題中沒有任何用處。如果你想要答案,我建議你再試一次。 – EJP 2012-08-14 10:20:01

回答

2

您似乎在刪除時再次寫入學生條目。

file.write(studentDelete.toString().getBytes()); 

也許你需要零出這些字節,或空白它們或一些特別的東西,這意味着該項目已被刪除。

+0

我嘗試了一些東西,如file.write(temp);當temp是「」,並將其設置爲null或零時,這樣的幾件事情。但輸出總是相同的:S – Pendo826 2012-08-14 10:20:26

+1

這意味着你沒有按照你想象的那樣寫很多字節。空字符串不會寫入任何內容。你需要做一些事情,比如'write(new byte [recordLength]);' – 2012-08-14 10:23:59

+0

Theres改進。在輸出中有一個學生姓名安迪·卡羅爾的副本,現在被刪除,但我想首先刪除的那個仍然存在:( – Pendo826 2012-08-14 10:28:55