0

Hey im having a problem. I want to use an arrayList to make a studentStore(I have this done) but when im trying to write the student using RandomAccessFile im clueless.

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");

These are my students

And now i need a way to write them to

RandomAccessFile raf = new RandomAccessFile("employee.dat", "rw");
Student er = new Student(null, null, null, null);
for (int i = 0; i < details.length; i++) //This is getting an error obviously because details wont be taken.  nor will a, b,c , d or e which is what the student was declared at the top.
{
    er.setFirstName([i]);
    er.setLastName(lnames[i]);
    er.setAddress(addresses[i]);
    er.setAge(ages[i]);
    er.setSalary(salaries[i]);
    er.write(raf);
}
raf = new RandomAccessFile("employee.dat", "rw");

er = new Student();

int numRecords = (int) raf.length() / er.size();

for (int i = 0; i < numRecords; i++) {
    er.read(raf);

    System.out.print(er.getStudentName() + " ");
    System.out.print(er.getStudentId() + " ");
    System.out.print(er.getStudentEmail() + " ");
    System.out.print(er.getStudentTelephoneNumber() + " ");
}
raf.seek(0);
for (int i = 0; i < numRecords; i++) {
    er.read(raf);
    if (er.getAge() >= 55) {
        er.setSalary(0.0);
        raf.seek(raf.getFilePointer() - er.size());
        er.write(raf);
        raf.seek(raf.getFilePointer() - er.size());
        er.read(raf);
    }
    System.out.print(er.getStudentName() + " ");
    System.out.print(er.getStudentId() + " ");
    System.out.print(er.getStudentEmail() + " ");
    System.out.print(er.getStudentTelephoneNumber() + " ");
}
Baz
  • 36,440
  • 11
  • 68
  • 94
Pendo826
  • 1,002
  • 18
  • 47
  • What's the problem? please be specific. – Bharat Sinha Aug 05 '12 at 11:20
  • The type of the expression must be an array type but it resolved to Student. This is the error. – Pendo826 Aug 05 '12 at 11:20
  • @BharatSinha The problem is that i do not know a way to get the students to print out using RandomAccessFile. I have googled all morning and i have not found out how to do this. – Pendo826 Aug 05 '12 at 11:21
  • You say you want to use `ArrayList`. What exactly is `StudentStore`? – Baz Aug 05 '12 at 11:23
  • The StudentStore is where the ArrayList is declared and where i am storing methods to edit the arrayList would you like to see it? – Pendo826 Aug 05 '12 at 11:25
  • @Pendo826 No, that's ok. Does `StudentStore` have a public field `length`? Moreover, you use the method `er.write(raf);` to write to the file? Seeing this method would most probably help. – Baz Aug 05 '12 at 11:28
  • Nope it doesnt have a length field but .length is used to get the size of the ArrayList is it not ? – Pendo826 Aug 05 '12 at 11:29
  • @Pendo826 No, it's `list.size();` But calling this on your `StudentStore` will not automatically call it on the contained `ArrayList` unless you do it manually. `.length` is used for simple arrays or strings. – Baz Aug 05 '12 at 11:30
  • Oooh right i understand. So have you got any suggestions on how i could write the students another way maybe. – Pendo826 Aug 05 '12 at 11:32
  • @Pendo826 First try to get the for loop working. Afterwards you can take care of the actual writing. – Baz Aug 05 '12 at 11:33
  • So do you think i should manually get the list.length? – Pendo826 Aug 05 '12 at 11:35
  • It's not `list.length`, it's `list.size()`. If you want the size of the arraylist within your studentstore, the studentstore has to have a method `size()` which returns `list.size()`. – Baz Aug 05 '12 at 11:36
  • Well thats easy enough to do :0 – Pendo826 Aug 05 '12 at 11:37

1 Answers1

2

Try this for your StudentStore to get the size of the inner ArrayList.

public class StudentStore
{
    ArrayList<Student> students = null;

    public StudentStore()
    {
        students = new ArrayList<Student>();
    }

    public int size()
    {
        return (students == null) ? 0 : students.size();
    }
}

However, I do not understand, why you have to create your own wrapped ArrayList instead of using a simple ArrayList...

Baz
  • 36,440
  • 11
  • 68
  • 94
  • Im just used to making a store this way. Ive done that and its asking me to create the method in student rather than student store. – Pendo826 Aug 05 '12 at 11:43
  • @Pendo826 Ok, if you think I was of any help, consider accepting my answer. Otherwise, create your own answer and accept that one. – Baz Aug 05 '12 at 11:47
  • er.setStudentName(a[i]); how would i get this part to work. a is opposed to Student a = new Student("Becky O'Brien", "DKIT26", "0876126944", "bexo@hotmail.com"); – Pendo826 Aug 05 '12 at 11:48
  • So `er` is a `Student` and `a` is a `Student`, too ? If so, you cannot use `a[i]`. – Baz Aug 05 '12 at 11:50