0

Hey my RandomAccessFile application is not working well for my deleteByName method. Basically i want this method to search the store for the Student and then remove it from the RandomAccessFile. The search bit is no problem but the RandomAccessFile still shows the employee with the details being null instead of deleting the student entirely. Can anyone help ?

Here is my code :

//---------------------------------------------------------------------------------------
//          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;

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

The store is made up using a hashmap.

Pendo826
  • 1,002
  • 18
  • 47
  • 1
    Your title could hardly be more confusing. Methods aren't in files and cannot therefore be removed from them. `DeleteByName()` is just the name of a part of your code, it isn't any use in a title. I suggest you try again if you want answers. – user207421 Aug 14 '12 at 10:20

1 Answers1

2

You appear to be writing the student entry again on a delete.

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

Perhaps you need to zero out these bytes, or blank them or something special which means the entry has been deleted.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I tried a few things like file.write(temp); when temp was "" and setting it to null or zero a few things like that. But the output is always the same :S – Pendo826 Aug 14 '12 at 10:20
  • 1
    That means you are not writing as many bytes as you thought. Empty string won't write anything. You need to do something like `write(new byte[recordLength]);` – Peter Lawrey Aug 14 '12 at 10:23
  • Theres an improvement. in the output there was a duplicate of the student name Andy Carroll and now that is deleted but the one i was trying to delete first of all is still there :( – Pendo826 Aug 14 '12 at 10:28
  • If there is a duplicate, that means you are writing it twice. This will only clear one entry. ;) I would try improving your unit tests because without those you are likely to go around in circles trying to fix this. – Peter Lawrey Aug 14 '12 at 10:31