forum

Home / DeveloperSection / Forums / Converting a text file to an array in Java

Converting a text file to an array in Java

Pravesh Singh 2011 29-Dec-2014
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);
    scanner.useDelimiter(',');
    while (scanner.hasNext()) {
        String name = scanner.next();
        String age = scanner.next();
        String income = scanner.next();
        String smart = scanner.next();
        Student students = new Student(name, age, income, smart);

        System.out.println(students);
    }
}
I just want to know the easiest way to go about this. I'm so close, I can feel it! Thanks in advance.

Updated on 30-Dec-2014

Can you answer this question?


Answer

1 Answers

Liked By