gpt4 book ai didi

java - DeleteByName 方法未从 randomAccessFile 中删除

转载 作者:行者123 更新时间:2023-12-02 07:37:54 27 4
gpt4 key购买 nike

嘿,我的 RandomAccessFile 应用程序不适用于我的 deleteByName 方法。基本上我希望这个方法在商店中搜索学生,然后将其从 RandomAccessFile 中删除。搜索位没有问题,但 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;

学生商店

// ---------------------------------------------------------------------------------------
// 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.
}

菜单方法

//---------------------------------------------------------------------------------------
// 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 组成的。

最佳答案

您似乎在删除时再次写入学生条目。

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

也许您需要将这些字节清零,或将其留空或执行一些特殊操作,这意味着该条目已被删除。

关于java - DeleteByName 方法未从 randomAccessFile 中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11950091/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com