0

Is it posible to put eachline (from txt file) to sepperate arraylist etc and put them incide Array DATABASE

I have a file with info of students something like that

Name Lastname yearsOld address BEN DDD 12 14Drive Olga Benar 12 23Ave

So in result I would have an ArrayLIST with arraylist(of each student)?

3
  • What do you want to occupy the arraylist of students, strings with their information? Commented Mar 30, 2014 at 23:27
  • I have a txt file with each line has student info Later on I need to search them using contains method (so I was going to put each student int o seperat arrayList and into one ArrayList where all students are) Commented Mar 30, 2014 at 23:29
  • I'm looking for same thing but in STRING stackoverflow.com/questions/12523497/… Commented Mar 30, 2014 at 23:34

1 Answer 1

1

How about creating a model of your data? E.g:

class Student {
    String name;
    String lastname;
    int yearsOld;
    ...more fields...
}

And use that to load your data into an ArrayList<Student>.

EDIT: However, to answer your question. Use the IO library to read the file line by line:

public List<List<String>> processFile(String file) throws IOException {      
    List<List<String>> data = new ArrayList<List<String>>();
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;
    while ((line = br.readLine()) != null) {
       data.add(processLine(line));
    }
    br.close();
    return data;
}

public List<String> processLine(String line) {
    return Arrays.asList(line.split(" ")); //  Be aware of spaces in names, addresses etc.
}
Sign up to request clarification or add additional context in comments.

3 Comments

but how do I put each line into seperate arraylist?
I belive there is suntax error somewhere... cant work
This is object oriented programming. Store in your arraylist objects of type Student as suggested. Then you can iterate over the arraylist and discover the details as required.

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.