# Reading data from .TXT file

**URL:** <https://discourse.processing.org/t/reading-data-from-txt-file/11695>\
**Category:** Coding Questions\
**Created:** [May 29, 2019, 9:14pm UTC](https://discourse.processing.org/t/reading-data-from-txt-file/11695 "2019-05-29T21:14:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![GuilhermeVolker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/guilhermevolker/32/5657_2.png) [@GuilhermeVolker](https://discourse.processing.org/u/GuilhermeVolker)\
**Post date:** [May 29, 2019, 9:14pm UTC](https://discourse.processing.org/t/reading-data-from-txt-file/11695/1 "2019-05-29T21:14:42Z")

</div>

I’am having trouble to read data from a file and store on variables. I already tryed several diferents approachs but none of them were sucssesful.  
The problem i’am having with this code is that i’am only reaciving data from the first line

```auto
float QNH, altitude;
float[] vals;
String[] str;

void setup(){
...
str = loadStrings("pressao.TXT");
  String all = join(str, " ");
  vals = float(split(all, " ")); 
}

void draw(){
int i = 0;
  int j = 1;
  altitude = vals[j];
  QNH = vals[i];
...
(several functions)
...
i+=2;
j+=2;
}

```

There are two values in each line, i want to save the first value on the variable QNH, the second one on Altitude.  
The data from the TXT looks like that:  
1021.24 3096.03  
1021.28 3097.36  
1021.28 3097.36  
1021.28 3097.36  
1021.28 3097.36  
1021.28 3097.36  
1021.28 3097.36  
1021.28 3097.36  
1021.28 3097.36  
1021.28 3097.36

---

<div class="post-metadata">

**Author:** ![ILikePi](https://avatars.discourse-cdn.com/v4/letter/i/7ab992/32.png) [@ILikePi](https://discourse.processing.org/u/ILikePi)\
**Post date:** [May 29, 2019, 9:20pm UTC](https://discourse.processing.org/t/reading-data-from-txt-file/11695/2 "2019-05-29T21:20:08Z")

</div>

Would a for loop work?

```auto
for (int i = 0, j = 1; j < *the amount of lines*; i+=2, j+=2){
 *the functions*
}

```

---

<div class="post-metadata">

**Author:** ![GuilhermeVolker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/guilhermevolker/32/5657_2.png) [@GuilhermeVolker](https://discourse.processing.org/u/GuilhermeVolker)\
**Post date:** [May 29, 2019, 9:25pm UTC](https://discourse.processing.org/t/reading-data-from-txt-file/11695/3 "2019-05-29T21:25:08Z")

</div>

could be more specific? the only place i can see to put a - for- is inside the draw function, but draw itself already do that.  
I think the code would have the same result 🤨

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 29, 2019, 9:25pm UTC](https://discourse.processing.org/t/reading-data-from-txt-file/11695/4 "2019-05-29T21:25:33Z")

</div>

> [@GuilhermeVolker](#):
>
> i want to save the first value on the variable QNH, the second one on Altitude.

there are multiple lines in the text file EACH containing 2 values

so QNH and Altitude should be arrays.

OR you’re only reading the very first 2 values (in the 1st line)

OR you overwrite both values QNH and Altitude every time with the values from the next line

---

<div class="post-metadata">

**Author:** ![GuilhermeVolker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/guilhermevolker/32/5657_2.png) [@GuilhermeVolker](https://discourse.processing.org/u/GuilhermeVolker)\
**Post date:** [May 29, 2019, 9:29pm UTC](https://discourse.processing.org/t/reading-data-from-txt-file/11695/5 "2019-05-29T21:29:12Z")

</div>

yeah, i’ll try the variables as array, could work. thanks

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 29, 2019, 9:29pm UTC](https://discourse.processing.org/t/reading-data-from-txt-file/11695/6 "2019-05-29T21:29:30Z")

</div>

> [@GuilhermeVolker](#):
>
> String all = join(str, " "); vals = float(split(all, " "));

I don’t think that’s correct

better for loop in draw

```auto
for(String s1 : str) {
   String[] str2 = split(s1, " "); 
   // now  
   QNH = float(str2[0]); 
   Altitude= float(str2[1]); 
}//for

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 29, 2019, 9:30pm UTC](https://discourse.processing.org/t/reading-data-from-txt-file/11695/7 "2019-05-29T21:30:20Z")

</div>

> i’ll try the variables as array, could work.

```auto
int i=0;
for(String s1 : str) {
   String[] str2 = split(s1, " "); 
   // now  
   QNH[i] = float(str2[0]); 
   Altitude[i] = float(str2[1]); 
   i++;
}//for

```

---

<div class="post-metadata">

**Author:** ![GuilhermeVolker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/guilhermevolker/32/5657_2.png) [@GuilhermeVolker](https://discourse.processing.org/u/GuilhermeVolker)\
**Post date:** [May 29, 2019, 10:57pm UTC](https://discourse.processing.org/t/reading-data-from-txt-file/11695/8 "2019-05-29T22:57:49Z")

</div>

> [@Chrisir](#):
>
> QNH[i] = float(str2[0]); Altitude[i] = float(str2[1]);

I’am receiving nullPointerException error on this part, any clue?  
(i’d declare the variables as array)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 30, 2019, 1:58am UTC](https://discourse.processing.org/t/reading-data-from-txt-file/11695/9 "2019-05-30T01:58:16Z")

</div>

Do you have

float[] QNH=new float[250];

?

---

<div class="post-metadata">

**Author:** ![GuilhermeVolker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/guilhermevolker/32/5657_2.png) [@GuilhermeVolker](https://discourse.processing.org/u/GuilhermeVolker)\
**Post date:** [May 30, 2019, 2:23am UTC](https://discourse.processing.org/t/reading-data-from-txt-file/11695/10 "2019-05-30T02:23:55Z")

</div>

i didan’t. I don’t have ideia how, but it works haha. Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 30, 2019, 6:20am UTC](https://discourse.processing.org/t/reading-data-from-txt-file/11695/11 "2019-05-30T06:20:36Z")

</div>

> I don’t have idea how

This `float[] QNH=new float[250];` just means: make a new array of the type float with 250 slots

There is a tutorial on arrays and one on data

> **[Arrays](https://www.processing.org/tutorials/arrays/)**
>
> How to store and access data in array structures.

[https://www.processing.org/tutorials/data/](https://www.processing.org/tutorials/data/)

Also one about Strings

> **[Strings and Drawing Text](https://www.processing.org/tutorials/text/)**
>
> Learn how use the String class and display text onscreen.
