# Reading a file with createReader

**URL:** <https://discourse.processing.org/t/reading-a-file-with-createreader/40499>\
**Category:** Coding Questions\
**Created:** [January 7, 2023, 4:20pm UTC](https://discourse.processing.org/t/reading-a-file-with-createreader/40499 "2023-01-07T16:20:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pacoruiz](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@pacoruiz](https://discourse.processing.org/u/pacoruiz)\
**Post date:** [January 7, 2023, 4:20pm UTC](https://discourse.processing.org/t/reading-a-file-with-createreader/40499/1 "2023-01-07T16:20:30Z")

</div>

Hi Im trying to make a code that read data from a file. I have just copy the code from the reference page, but it doesnt work. It read an empty line after the end of the file. Don’t know why.  
My file has this:  
3,6  
4,8  
And the error is ArrayIndexOutOfBoundException.

Thenks  
\<void setup() {  
size(100, 100);  
parseFile();  
}

void parseFile() {  
// Open the file from the createWriter() example  
BufferedReader reader = createReader(“datos.txt”);  
String line = null;  
int nums = new int[3];  
try {  
while ((line = reader.readLine()) != null) {  
println(line);  
nums = int(split(line, ‘,’));  
printArray(nums);  
int x = nums[0];  
int y = nums[1];  
println(x);  
}  
reader.close();  
} catch (IOException e) {  
e.printStackTrace();  
}  
} /\>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [January 7, 2023, 5:17pm UTC](https://discourse.processing.org/t/reading-a-file-with-createreader/40499/2 "2023-01-07T17:17:39Z")

</div>

The following runs without error on my system. I changed the way that you set up the int array.

```auto
int[] nums;

void setup() {
  size(100, 100);
  parseFile();
}

void parseFile() {
  // Open the file from the createWriter() example
  BufferedReader reader = createReader("data.txt");
  String line = null;
  nums = new int[3];
  try {
    while ((line = reader.readLine()) != null) {
      println(line);
      nums = int(split(line, ','));
      printArray(nums);
      int x = nums[0];
      int y = nums[1];
      println(x);
    }
    reader.close();
  }
  catch (IOException e) {
    e.printStackTrace();
  }
}

```
