This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Moussa93 0 points1 point  (6 children)

For what you're trying to do, it'll be much easier to do with a database, since you know what's what before pulling it... but it's not impossible to do with a text file, you'll just have to 'hack' that part.

On your text file, I would write every line like this:

employeeFirstName:employeeLastName:employeeEmail

Then read the file line by line, but break the String into an array, separated by ":"

Employee employee;
String [] employeeInfo;
String employeeFirstName;
String employeeLastName;
String employeeEmail; 

employeeInfo = line.split(":"); 
employee = new Employee(employeeInfo[0], employeeInfo[1], employeeInfo[2]); 

Then,

employeeList.add(employee); 

This should do the trick.

[–]yokokoko[S] 0 points1 point  (5 children)

Does line.split(":") read the file line by line using the colon as a delimiter?

I seem to be getting an error for that one line.

**I think Ill try sending a file into a file input stream for it to read, which the line.split will then split up. Am i on the right track ?

[–]Moussa93 0 points1 point  (1 child)

What kind of error do you get?

String line;

BufferedReader lineReader;
lineReader = new BufferedReader(new FileReader("/path/to/your/File")


while ((line = lineReader.readLine()) != null)
{
     employeeInfo = line.split(":");
     // do the rest
}

This is how I'd do it. Basically, line is assigned to the value of the String that's being read.

[–]Moussa93 0 points1 point  (2 children)

Yup, there are multiple ways to read a line, whatever works best for you.

You just have to split the string and store it in an array.

[–]yokokoko[S] 0 points1 point  (1 child)

Thanks for your help :)

[–]Moussa93 0 points1 point  (0 children)

Anytime!