Converting a text file to an array in Java

I'm a beginner at Java and have a list of 25 students that include their name, age, income and IQ in a text file. I'm struggling with how to take this text file and put it in an Array so that I can sort them and such. So far I have:

File myFile = new File ("./src/Project2/StudentList");
Scanner myScan = new Scanner(myFile);
while (myScan.hasNext()) {
    String line = myScan.nextLine();
    Scanner scanner = new Scanner(line);
    while (scanner.hasNext()) {
        String name =;
        String age =;
        String income =;
        String smart =;
        Student students = new Student(name, age, income, smart);

I just want to know the easiest way to go about this. I'm so close, I can feel it! Thanks in advance.
Last updated:12/30/2014 1:01:51 AM

1 Answers

Royce Roy
Royce Roy

Define your array:

Student[] students = new Student[25];
int i = 0;
then in your loop

Student student = new Student(name, age, income, smart);
students[i++] = student;
or dynamic array

List<Student> students = new ArrayList()<>;
and in loop:

Student student = new Student(name, age, income, smart);