Hi,
I have a problem with a java application which should use ArrayLists, and I would like to ask if you can help me.

I have created a Person class, and 2 subclasses, Student and Employee. Data attributes common are Name, SSN (social security number), and Age.
A student has a grade-point average (GPA), and Major.
An employee is hourly_employee (hourly_rate, hours_worked) and salaried _employee(salary).

A file is provided:

======================================================
    Name        SSN Age GPA Major   Salary  Hourly_rate Hours_worked    
------------------------------------------------------------------------------------------
1.  John Doe    123 30  4.0 BIO N/A     N/A N/A
2.  Mary Ann    456 40  N/A N/A 30000       N/A N/A
3.  Lucy        789 25  N/A N/A N/A     15      200
======================================================

The program should load the list from that file using Arraylist. I should provide a method to retrieve the information for a particular person identified by his/her SSN.

I have created a file Person.java

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;

public class Person
{
    public String name;
    public float SSN;
    public int age;


    public void SetName(String n){ 
        name = n ;
}

    public String getName(){
        System.out.println("Person Name: "+ name);
        return name;
    }
}

class Student extends Person
{
    public float GPA;
    public float getGPA(){
        System.out.println("GPA" +GPA);
        return GPA;
    }
    public String getName(){
        System.out.println("Student Name: "+ name);
        return name;
    }
}

class Employee extends Person
{
    public String getName(){
        System.out.println("Employee Name: "+ name);
        return name;
    }
}

and ReadFile.java

import java.util.ArrayList;
import java.io.*;

public class ReadFile {
    public static void main(String args[]) {
        // Lets create an arrayList object full of String types.
        ArrayList <Person> arrayOfSDtrings = new ArrayList<Person>();

        try {
        // Create our bufferedreader to read the file
BufferedReader reader = new BufferedReader(new FileReader("c:\\input.txt"));

        // Line to hold the line read from file
            //String line = "";
            String str;

// Loop through the file reading in lines and storing in "line". 
//Do this until readLine returns null (end of file)
            // Add each line to our arraylist object
            while ((str = reader.readLine()) != null) {
                Person.add(new Student);
            }

// Now use a foreach style loop to read each string in our arraylist. 
//Notice we don't have to cast to string because we told 
//the arraylist that it will hold strings at the start.
            for (String content : Person) {
        //input.txt.Split(".");
                System.out.println(content);
            }

            reader.close();
        }
catch (Exception ex) { System.out.println("Exception: " + ex.getMessage()); }
    }
}

I would like to ask how to load the data from the file to an ArrayList structure, and how to search through the ArrayList created.
I would like to thank you in advance.
George Gardu

First sort out the references to the ArrayList containing Strings - it doesn't - it contains Students.
Then, this is the plan:
Read each line from the file.
Split each line into its individual fields.
Check which fields are N/A so you know what kind of Person this is.
Create a new Employee or Student as appropriate using the parsed fields.
Add the Employee or Student to the ArrayList.

As for searching, just loop through the elements of the ArrayList checking each one against the search criteria until you get one that matches.

There are a few other (small) problems with your code, but you should be able to sort those out once you get something running.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.