0

I am currently writing a program to read in data from a text file and utilize the data in some way. So far I can read the file no problem but the issue I am having is with what comes next. As the data is read from the text file the appropriate objects must be created, from classes that I have already built, and stored in 2 arrays based upon the data. As I said before I have the code for the data to be read completed but I do not know how to use that data to create objects and to store those objects into arrays.

Here is the code that I have so far in the main method:

public static void main(String[] args) {
        BufferedReader inputStream = null;

        String fileLine;
        try {
            inputStream = new BufferedReader(new FileReader("EmployeeData.txt"));

            System.out.println("Employee Data:");
            // Read one Line using BufferedReader
            while ((fileLine = inputStream.readLine()) != null) {
                System.out.println(fileLine);
            }//end while
        } catch (IOException io) {
            System.out.println("File IO exception" + io.getMessage());
        }finally {
            // Need another catch for closing 
            // the streams          
            try {               
                if (inputStream != null) {
                inputStream.close();
            }                
            } catch (IOException io) {
                System.out.println("Issue closing the Files" + io.getMessage());
            }//end catch
        }//end finally
    }//end main method
2
  • Inside the while loop you can create the objects and add them to the arrays. Commented Jan 20, 2019 at 5:30
  • 1
    What exactly is the difficulty you face in creating objects and adding them to the arrays? Commented Jan 20, 2019 at 5:31

1 Answer 1

2

You have to think about how data are represented in the text file and map them accordingly to the Employee class.

Take for instance if the Employee class is as below -

class Employee {
   String firstName;
   String lastName;

}

and the lines in the file are like -

first1 last1
first2 last2

You can create an arrayList of Employee to hold the data -

List<Employee> employees = new ArrayList();

When you read each line from the file, you can split the line by space, construct the object and add to the list -

String[] name = fileLine.split(" ");
Employee e = new Employee();
e.firstName = name[0];
e.lastName = name[1];

employees.add(e);

So basically, you have to consider the structure of the data in your source (text file) and figure out how you would parse them and construct your desired object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.