---
title: "Converting a text file to an array in Java"  
description: "Converting a text file to an array in Java"  
author: "Pravesh Singh"  
published: 2014-12-29  
updated: 2014-12-30  
canonical: https://www.mindstick.com/forum/12828/converting-a-text-file-to-an-array-in-java  
category: "java"  
tags: ["java", "file", "array"]  
reading_time: 2 minutes  

---

# Converting a text file to an array in Java

I'm a [beginner](https://www.mindstick.com/forum/23159/need-beginner-project-ideas) at [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) and have a list of 25 [students](https://www.mindstick.com/articles/13083/benefits-of-students-who-can-get-online-assignment) that include their [name](https://yourviews.mindstick.com/view/87450/how-to-generate-a-brand-name-using-linkedin-comprehensive-guide), [age](https://www.mindstick.com/articles/23262/the-modern-age-these-technology-trends-can-make-your-life-easier), income and IQ in a [text file](https://www.mindstick.com/forum/33855/create-and-insert-data-to-a-text-file-in-android). I'm struggling with how to take this text file and put it in an [Array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net) 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](https://yourviews.mindstick.com/story/1687/rubbing-hands-close-up-benefits), I can feel it! Thanks in advance.

## Replies

### Reply by 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);students.add(student);


---

Original Source: https://www.mindstick.com/forum/12828/converting-a-text-file-to-an-array-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
