0

Hey i am stuck on an add method for my ArrayList store students which then writes the student store to a text file using randomAccessFile. So i need to be able to add a student to the store and then write it to the file. Can anyone suggest a way i can do this ? I can add the student to the store no problem but the actual problem is that i am writing the students to the file using a static array and im not sure how i would go about letting the user add the new employee to the static array?

Here is my code:

MainApp

import java.io.IOException;
import java.io.RandomAccessFile;
public class MainApp
{

    public static void main(String[] args) throws Exception 
    {
        new MainApp().start();

    }
    public void start()throws Exception 
    {
        StudentStore details = new StudentStore();
        Student a = new Student("Becky O'Brien", "DKIT26", "0876126944", "bexo@hotmail.com");
        Student b = new Student("Fabio Borini", "DKIT28", "0876136944", "fabioborini@gmail.com");
        Student c = new Student("Gaston Ramirez", "DKIT29", "0419834501", "gramirez@webmail.com");
        Student d = new Student("Luis Suarez", "DKIT7", "0868989878", "luissuarez@yahoo.com");
        Student e = new Student("Andy Carroll", "DKIT9", "0853456788", "carroll123@hotmail.com");
        details.add(a);
        details.add(b);
        details.add(c);
        details.add(d);
        details.add(e);
        //details.print();


        RandomAccessFile file = new RandomAccessFile("ContactDetails.txt","rw");
        //getBytes() returns an array of bytes.
        //Because i have put the store in a static Array.(I done this because i could find no other
        //Simple way to write a Student Object.)
        //None of the methods of the RandomAccessFile write class worked with this.
        Student[] students = {a,b,c,d,e};
        details.write(students, file);
        details.readAll(file);



        file.close();


     }


 }

StudentStore

//---------------------------------------------------------------------------
//Imports.
//---------------------------------------------------------------------------   
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
//---------------------------------------------------------------------------   

public class StudentStore
{
//---------------------------------------------------------------------------
//ArrayList declaration.
//---------------------------------------------------------------------------
    List<Student> students = new ArrayList<Student>();
//---------------------------------------------------------------------------
//Name:          Add method.
//Description:   Adds a student to the ArrayList.
//---------------------------------------------------------------------------
    public void add(Student student) 
    {
        students.add(student);
    }
//---------------------------------------------------------------------------
//Name:          DeleteAll method.
//Description:   Delete's everything in the ArrayList.
    //---------------------------------------------------------------------------
     public void deleteAll()
     {
           students.clear();
     }
//---------------------------------------------------------------------------
//Name:          Print method.
//Description:   Prints out the contents of the ArrayList.
//---------------------------------------------------------------------------
    public void print() 
    {
        for (int i = 0; i < students.size(); i++) 
        {
          Student a = students.get(i);
            System.out.println(a.toString());
        }
    }
    public int size()
    {
        return (students == null) ? 0 : students.size();
    }
    public void write(Student[] students, RandomAccessFile file) throws IOException
    {
        for (int i = 0; i < students.length; i++)
        {
        byte[] bytes = students[i].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);
    }

}

Note: I havent shown the student class because it is built up of a constructor, getters and setters and a toString and i didnt feel the use of uploading it but if needed i will gladly do it.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Pendo826
  • 1,002
  • 18
  • 47
  • 1
    In your write method, should you be iterating through the ArrayList instead of passing in an array as a parameter? – Aaron Kurtzhals Aug 07 '12 at 21:18
  • Im passing in the array as a parameter so that i could use the method in the mainApp without having lots of methods down the bottom of my main. – Pendo826 Aug 07 '12 at 21:28
  • 1
    I'd probably use a static `List` instead of an array. You "could" look at the `Arrays` class http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html as it has methods for copying arrays as well as `System.copyArray` http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy(java.lang.Object,%20int,%20java.lang.Object,%20int,%20int), but to be honest, `ArrayList` does this for you anyway – MadProgrammer Aug 07 '12 at 21:34
  • 1
    When you say 'employee', do you mean another class, or another instance of Student. I couldn't really understand what you are trying to achieve, and where are you having trouble – La bla bla Aug 07 '12 at 21:42
  • Apologies employee is student. I was recently working on another project with employees and it is stuck in the brain i will edit it now. – Pendo826 Aug 07 '12 at 21:50

1 Answers1

1

Personally I would just write the arraylist to a file instead of creating a separate array to have your write method write to file. This would also simplify your main method. You can see a good example here

So, your main would look something like this:

StudentStore details = new StudentStore();  
details.add(new Student("Becky O'Brien", "DKIT26", "0876126944", "bexo@hotmail.com"));  
// add more students  
details.write(new RandomAccessFile("ContactDetails.txt","rw"));

You could also simplify it further and just have the only parameter for the write method be a filename.

Community
  • 1
  • 1