# splitTokens() question

**URL:** https://discourse.processing.org/t/splittokens-question/17076
**Category:** Electronics (Arduino, etc.)
**Created:** [January 14, 2020, 5:18am UTC](https://discourse.processing.org/t/splittokens-question/17076 "2020-01-14T05:18:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![pandl](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pandl](https://discourse.processing.org/u/pandl)
#### Post date: [January 14, 2020, 5:18am UTC](https://discourse.processing.org/t/splittokens-question/17076/1 "2020-01-14T05:18:05Z")

</div>

Thank you for reading this.

The following is a section from a piece of code that I copied for an Internet project. It fails with an exception error on the line float hum = float(list[1]);. Some Internet searching reveals that this might be a case for splitTokens(). Is that correct and if so how I proceed?

```auto
  if (port.available() > 0)
  {
    String val = port.readString(); // read incoming string on serial port
    // First we need to separate temperature and humidity values
    String[] list = split(val, ','); // splits value separated by ','
    float temp = float(list[0]); // first value is Temperature
    float hum = float(list[1]); // second value is Humidity

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 14, 2020, 7:19am UTC](https://discourse.processing.org/t/splittokens-question/17076/2 "2020-01-14T07:19:21Z")

</div>

only ask for data you know they exist,

`if ( list.length >= 2 )` tells you if you have actually 2 values, OR a BAD LINE

* * *

also better use 2 more checks:

- trim()
- if ( val != null )

see also

> **[arduino\_CSV\_line ($1928961) · Snippets](https://gitlab.com/snippets/1928961)**
>
> GitLab.com

---

<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: [January 14, 2020, 7:44am UTC](https://discourse.processing.org/t/splittokens-question/17076/3 "2020-01-14T07:44:40Z")

</div>

> [@\[SOLVED\] ArrayIndexOutOfBoundsException: 1](https://discourse.processing.org/t/solved-arrayindexoutofboundsexception-1/2288/7):
>
> For multiple data transmission, I recommend you to read this post below: electric_plug[Forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item\_1](http://Forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_1) As you can see, on the Arduino side, separate the data w/ \t using print() + write(). And then send the last data w/ println(). On the Processing side, inside serialEvent(), use Serial::readString() + PApplet.splitTokens() in order to get all the data as a String[] array. Of course, you’re still gonna need to conver…

> [@Using two different readStringUntil "characters"](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/6):
>
> The solution I gave on that link expects the Arduino C code to send the multi-data as if they were a row of tab-separated values (TSV): face_with_monocle That is, each value is followed by a \t character; except last 1, which is followed by an \n instead: nerd_face Serial.print(temperature); Serial.write('\t'); Serial.print(masse); Serial.write('\t'); Serial.print(humidite); Serial.write('\t'); Serial.print(comptage); Serial.write('\t'); Serial.println(son); // implies '\n' At the Pro…

---

<div class="post-metadata">

### Author: ![pandl](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pandl](https://discourse.processing.org/u/pandl)
#### Post date: [January 15, 2020, 4:12am UTC](https://discourse.processing.org/t/splittokens-question/17076/4 "2020-01-15T04:12:54Z")

</div>

> [@kll](#):
>
> `if ( list.length >= 2 )`

Thank you kll, I should have thought of this myself but I had convinced myself that split function was the problem.

---

<div class="post-metadata">

### Author: ![pandl](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@pandl](https://discourse.processing.org/u/pandl)
#### Post date: [January 15, 2020, 4:15am UTC](https://discourse.processing.org/t/splittokens-question/17076/5 "2020-01-15T04:15:30Z")

</div>

Two interesting posts that I have kept for future reference, thank you GoToLoop.
