# Parsing points from a text file

**URL:** <https://discourse.processing.org/t/parsing-points-from-a-text-file/36611>\
**Category:** Coding Questions\
**Created:** [April 26, 2022, 5:20pm UTC](https://discourse.processing.org/t/parsing-points-from-a-text-file/36611 "2022-04-26T17:20:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaron\_Barber272](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Aaron\_Barber272](https://discourse.processing.org/u/Aaron_Barber272)\
**Post date:** [April 26, 2022, 5:20pm UTC](https://discourse.processing.org/t/parsing-points-from-a-text-file/36611/1 "2022-04-26T17:20:41Z")

</div>

PVector[] globe;  
float x = 0;  
float y = 0;  
float z = 0;  
import peasy.\*;

PeasyCam cam;

void setup() {  
size(600, 600, P3D);  
cam = new PeasyCam(this, 500);  
globe = new PVector[30];  
stroke(255);  
lights();  
}  
void draw(){  
String[] lines = loadStrings(“Points.txt”);  
String All\_Points = join(lines, " ");  
String[] P = splitTokens(All\_Points, ", ");  
println(All\_Points);  
for (int i=0 ; i\<30 ; i = i+3) {  
x = float(P[i]);  
println(x);  
}  
for (int j=1 ; j\<30 ; j = j+3) {  
y = float(P[j]);  
println(y);  
}  
for (int k=2 ; k\<30 ; k = k+3) {  
z = float(P[k]);  
println(z);  
}  
for (int i=0 ; i\<10 ; i++){  
globe[i] = new PVector(x,y,z);  
}  
for (int i=0 ; i\<10 ; i++){  
PVector v1 = globe[i];  
stroke(255);  
strokeWeight(4);  
point(v1.x, v1.y, v1.z);  
scale(5.0);  
}  
}

I am new to code, so I’m not sure what is going wrong. I have a text file which contains a series of points in different lines. I need to convert every group of three points into PVectors so that I can draw them as points in the canvas. The points are in rectangular coordinates, so it would be nice to be able to convert them once and then draw the (x,y,z) representation of the points.

The text file is organized as:  
1, 1.5708, 0,  
1.4142, 0.7854, 0,  
1, -1.5708, 0,  
1.4142, 0.7854, 0,  
1, 1.5708, 1,  
1.4142, 0.7854, 1,  
1, -1.5708, 1,  
1.4142, 0.7854, 1,  
1, -1.5708, 2,  
1, 1.5708, 2

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 27, 2022, 12:54am UTC](https://discourse.processing.org/t/parsing-points-from-a-text-file/36611/2 "2022-04-27T00:54:22Z")

</div>

Hello @Aaron_Barber272 ,

Please format your code as a courtesy to the community:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

Each of your loops ends with x, y, and z set to the last execution of that loop and each element of _globe[]_ is the same.

You can do most of this in _setup()_ and use the final array(s) you need in _draw()_.

Try this in one loop which acts on each element:

```auto
String lines [] = loadStrings("Points.txt");

float[] pv;
PVector[] globe = new PVector[10];

int i = 0;
pv = float(splitTokens(lines[i], ","));
globe[i] = new PVector(pv[0], pv[1], pv[2]);
print(globe[i].x, globe[i].y, globe[i].z);

```

In a loop (you can add one) it outputs:

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/5/f/5f3017516db30399450bc1ed0a03fe93cd4a7d47.png)

10 points plotted (2 are duplicates):

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/b/6/b63d70686f67fb198e3aa314f5b62182294e7301.png)

I used this instead of _scale()_:

> **[Reference](https://processing.org/reference/PVector_mult_.html)**
>
> Multiplies a vector by a scalar. The version of the method that uses a float acts directly on the vector upon which it is called (as in the first example above). The versions that receive both a PV…

`:)`
