# Splitting a String into an Array

**URL:** <https://discourse.processing.org/t/splitting-a-string-into-an-array/22495>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [July 9, 2020, 6:45am UTC](https://discourse.processing.org/t/splitting-a-string-into-an-array/22495 "2020-07-09T06:45:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![CKMJPEG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ckmjpeg/32/9899_2.png) [@CKMJPEG](https://discourse.processing.org/u/CKMJPEG)\
**Post date:** [July 9, 2020, 6:45am UTC](https://discourse.processing.org/t/splitting-a-string-into-an-array/22495/1 "2020-07-09T06:45:44Z")

</div>

Hi Processing friends,

I have a string .txt file that has 6 columns of data, which corresponds to x,y,z,r,g,b

I want to load the string file and parse the data into an array.

I would like to have the data as an array [x][y][z].

and an array seperately for [r][g][b].

for example, one line of my .txt file looks like this:  
12 18 -19 124 119 113  
This is from a point cloud .txt file output.

```auto
float[][] allPoints;
  float[] centroid = {0, 0, 0};
  PShape pixelCloud;
  
  void setup() {
      
    String[] lines = loadStrings("grid.txt");    
    println("there are " + lines.length + " lines");
    allPoints = new float[lines.length][3];
  
    //Read through each line of the file
    for (int i = 0; i < lines.length; i=i+1) { 

```

This is what I have so far. I am a beginner, and I have searched through youtube and the reference libraries on the processing website, and I am failing to understand data types and how to accesses data within the array.

Thank you for your time!

Best regards,

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 9, 2020, 6:50am UTC](https://discourse.processing.org/t/splitting-a-string-into-an-array/22495/2 "2020-07-09T06:50:38Z")

</div>

[Processing.org/reference/loadTable\_.html](http://Processing.org/reference/loadTable_.html)

---

<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:** [July 9, 2020, 7:06am UTC](https://discourse.processing.org/t/splitting-a-string-into-an-array/22495/3 "2020-07-09T07:06:36Z")

</div>

Or use command split to get an array

Then you don’t have to use arrays

Use PVector and color data type instead

See reference for all 3 commands

---

<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:** [July 9, 2020, 1:47pm UTC](https://discourse.processing.org/t/splitting-a-string-into-an-array/22495/4 "2020-07-09T13:47:56Z")

</div>

> [@CKMJPEG](#):
>
> This is what I have so far.

This is not complete.

> [@CKMJPEG](#):
>
> ! am a beginner, and I have searched through youtube and the reference libraries on the processing website, and I am failing to understand data types and how to accesses data within the array.

We all have to start somewhere.  
Keep working at it!  
There are tutorials as well.

One of the best tools in a programmers tool chest is knowing the resources available to you and learning to navigate and use them.

This is a very short list:

> **Resources \< Click here to expand !**
>
> I encourage you to review the resources available here:
> 
> - The Processing _website_ has references, examples, tutorials, etc.  
> [https://processing.org/](https://processing.org/)
> - The Processing PDE (IDE) has lots to offer!  
> An exploration of the tabs will reveal what is available; libraries, tools and local examples there.
> - The Coding Train  
> [https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1\_aw](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw)  
> [https://thecodingtrain.com/](https://thecodingtrain.com/)
> - Happy Coding  
> [Processing Tutorials - Happy Coding](https://happycoding.io/tutorials/processing/)
> - The source code can give insight if you are adventurous:  
> [GitHub - processing/processing: Source code for the Processing Core and Development Environment (PDE)](https://github.com/processing/processing)  
> Many of the Processing functions are here:  
> [processing/core/src/processing/core/PApplet.java at master · processing/processing · GitHub](https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java)
> - The Processing Foundation  
> [https://processingfoundation.org/](https://processingfoundation.org/)  
> Check out the tabs.  
> In the education tab, there is a reference to the Coding Train.
> - And the Internet. `:)`

You will have to work through arrays first:

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

This was an excellent resource:  
[https://processing.org/tutorials/data/](https://processing.org/tutorials/data/)

> **[Java Data Types](https://www.w3schools.com/java/java_data_types.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

`:)`

I pieced together 5 lines from the data tutorial, created a simple file.txt and was able to achieve this:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/67e9df778c29861e7d35d3d688a7c0363ca59c48.png)

I am not sharing code.

`:)`

---

<div class="post-metadata">

**Author:** ![CKMJPEG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ckmjpeg/32/9899_2.png) [@CKMJPEG](https://discourse.processing.org/u/CKMJPEG)\
**Post date:** [July 13, 2020, 7:11am UTC](https://discourse.processing.org/t/splitting-a-string-into-an-array/22495/5 "2020-07-13T07:11:06Z")

</div>

Hello Processing Friends!

I have read your resources, and I was able to get some help, but I don’t fully understand how everything is working.

Could someone explain how this method works for accessing each index in the array? Intuitively, I can understand that because the .txt file is being split by the number of spaces in between each column of the file `String[] vals = lines[i].split(" ");` that each column becomes the array index i.e, 6 lines of code = 0-5 array index.

So, all you need to do is declare the float and the array index and that will be successful in storing the value?

In addition, in the `void setup()` could someone explain how the for statement stores integer values?

> int grid;  
> void importText(String fName) {  
> String lines = loadStrings(fName);  
> for (int i = 0; i \< lines.length; i+=1) {//edit point density  
> String vals = lines[i].split(" ");  
> float x = float(vals[0]);  
> float y = float(vals[1]);  
> float z = float(vals[2]);  
> float r = float(vals[3]);  
> float g = float(vals[4]);  
> float b = float(vals[5]);  
> PVector temp = new PVector(x, y, z);  
> int col = color(r, g, b, 255);  
> if (x \< dimx && x \>= 0 && y \< dimy && y \>= 0 && z \<dimz && z \>=0) {  
> grid[int(x)][int(y)][int(z)] = col;  
> }  
> }  
> }

> void setup() {  
> size(1920, 1080, P3D);  
> dataList = new ArrayList();  
> grid = new int[dimx][dimy][dimz];  
> for (int x = 0; x \< grid.length; x++) {  
> for (int y = 0; y \< grid.length; y++) {  
> for (int z = 0; z \< grid[y].length; z++) {  
> grid[y][z] = 0;  
> }  
> }  
> }  
> importText(“pointCloud.txt”);  
> }

Thank you so much for your time and help. 🙂

---

<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:** [July 13, 2020, 10:48am UTC](https://discourse.processing.org/t/splitting-a-string-into-an-array/22495/6 "2020-07-13T10:48:14Z")

</div>

**Explanation**

Yeah, `split()` gives you an array per line (!) and the slots of the arrays are (depending on your csv) the xyz and rgb values.

In general you‘re doing it right.

Instead of int for color use data type `color`. Same thing but more clear.

Here: `int col = color(r, g, b, 255);`

and here:

`int[][][] grid;`

**The grid**

You don’t have to use the grid[][][] at all though.

Instead have to parallel arrays, one of type PVector, other of type color and store your points in there. Then for loop over those two and display the content.

No grid needed.

The grid is much too slow and too big since you would have to for loop over a lot of empty cells which is time consuming.

And since you use a float position as an index (int) you could also get rounding errors.

**Remark**

(when you are into OOP make a class point with color and use sphere to display it)

**Remark**

It’s easier for us when you post the first 10 lines of your data file here formatted as code.

Chrisir

---

<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:** [July 13, 2020, 10:51am UTC](https://discourse.processing.org/t/splitting-a-string-into-an-array/22495/7 "2020-07-13T10:51:17Z")

</div>

> [@CKMJPEG](#):
>
> `String[] vals = lines[i].split(" ");`

In this line first note the i: the line number of your text file. We loop over the text file with i and look at each line separately (the `i`th line in the file).

split() breaks up the line. So `vals` hold the data / values of this line. So one point in the cloud.

---

<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:** [July 13, 2020, 12:38pm UTC](https://discourse.processing.org/t/splitting-a-string-into-an-array/22495/8 "2020-07-13T12:38:18Z")

</div>

> [@CKMJPEG](#):
>
> In addition, in the `void setup()` could someone explain how the for statement stores integer values?

it’s a 3fold nested for loop which means the outer one calls the inner one and the inner one the most inner one grid.length times.

So the most inner line grid[y][z] = 0; is executed `grid.length times * grid.length times * grid.length times`.

For the data structure int grid, read the tutorials on arrays (a list) and on 2D array (a grid like a chessboard). [Tutorials / Processing.org](https://www.processing.org/tutorials/)

This is a 3D array (like a pile of chessboards).

So the indexes say: take the xth row, yth column, zth level / etage in the pile of chess boards.

The order of the x,y,z can be discussed; for example, in 3D Sketches I see

- x as left / right
- y as the height above ground
- and z as back / forth

see [https://www.youtube.com/watch?v=37W7tJ6x2-w](https://www.youtube.com/watch?v=37W7tJ6x2-w)

Chrisir

---

<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:** [July 14, 2020, 1:53am UTC](https://discourse.processing.org/t/splitting-a-string-into-an-array/22495/9 "2020-07-14T01:53:31Z")

</div>

> [@CKMJPEG](#):
>
> Could someone explain how this method works for accessing each index in the array?

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

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

`:)`
