# Problems with converting a String to a float

**URL:** https://discourse.processing.org/t/problems-with-converting-a-string-to-a-float/28761
**Category:** Electronics (Arduino, etc.)
**Tags:** homework
**Created:** [March 23, 2021, 4:01pm UTC](https://discourse.processing.org/t/problems-with-converting-a-string-to-a-float/28761 "2021-03-23T16:01:13Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Nick2](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@Nick2](https://discourse.processing.org/u/Nick2)
#### Post date: [March 23, 2021, 4:01pm UTC](https://discourse.processing.org/t/problems-with-converting-a-string-to-a-float/28761/1 "2021-03-23T16:01:13Z")

</div>

Hi! Im trying to display my sensor temperature thats hooked up with the arduino.  
The project is basiclly to display temperature in little rectangles with the colors:RGB, but to do that I first have to split my String and then convert the second part of the String to a float while leaving the first part a String and giving it a new name.I have succesfully split my String and list[0] works fine (list[0] being the first part I was talking about), But for some reason whenever i try to use list[1] it keeps returning a ArrayIndexOutOfBoundsExeption: error. Could somebody help me with this? Here is the Code:

Processing code:

1 import processing.serial.\*;

2 Serial myPort;  
3 int lf = 10;  
4 float temp =0.1;  
5 String s =null

6 void setup() {  
7 myPort = new Serial(this, “COM5”, 9600);  
8 myPort.clear();  
9 s = myPort.readStringUntil(lf);  
}

10 void draw() {  
11 while (myPort.available() \> 0) {  
12 s = myPort.readStringUntil(lf);

13 if (s != null) {  
14 String[] list = split(s, “,”);

15 String sen1 = list[0];

16 float temp = float(list[1]);

## 17 delay(600); } } }

The ERROR occurs at line:16

---

<div class="post-metadata">

### Author: ![Nick2](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@Nick2](https://discourse.processing.org/u/Nick2)
#### Post date: [March 23, 2021, 4:01pm UTC](https://discourse.processing.org/t/problems-with-converting-a-string-to-a-float/28761/2 "2021-03-23T16:01:59Z")

</div>

> [@Nick2](#):
>
> ` 16 float temp = float(list[1]);`

That is where the error apears.

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [March 23, 2021, 4:36pm UTC](https://discourse.processing.org/t/problems-with-converting-a-string-to-a-float/28761/3 "2021-03-23T16:36:50Z")

</div>

If you have an out of bounds exception, it probably means that you try to access an element of the array that does not exist.

So my guess is that this line: `String[] list = split(s, ",");` does not behave the way you expect.

Did you print s in the console to see how it looks like?

Also can you please format your code with the `</>` icon? Thanks.

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [March 24, 2021, 9:02am UTC](https://discourse.processing.org/t/problems-with-converting-a-string-to-a-float/28761/4 "2021-03-24T09:02:50Z")

</div>

The first element index in an array is zero so if `s` does not contain a comma it will return a single element with the array index 0 so try

` float temp = float(list[0]);`

---

<div class="post-metadata">

### Author: ![Nick2](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@Nick2](https://discourse.processing.org/u/Nick2)
#### Post date: [March 24, 2021, 1:59pm UTC](https://discourse.processing.org/t/problems-with-converting-a-string-to-a-float/28761/5 "2021-03-24T13:59:23Z")

</div>

Yes! It works when I print: "print(list); " but when I try to convert it to a float the error appears.

---

<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: [March 24, 2021, 2:56pm UTC](https://discourse.processing.org/t/problems-with-converting-a-string-to-a-float/28761/6 "2021-03-24T14:56:20Z")

</div>

Can you please post your code for this?

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [March 24, 2021, 4:33pm UTC](https://discourse.processing.org/t/problems-with-converting-a-string-to-a-float/28761/7 "2021-03-24T16:33:04Z")

</div>

> [@Nick2](#):
>
> it keeps returning a ArrayIndexOutOfBoundsExeption: error.

Since the error is being generated here  
`16 float temp = float(list[1]);`

Then the problem is not caused by the conversion of a String to a float it is because the array length is less than 2 i.e. 0 or 1. It can’t be 0 because the exception would have been thrown by line 15. The only way I can see that happening is if the String `s` does not contain a comma.

Please show an example of `s` that generates this exception and the output from print list otherwise we are all working in the dark and having to guess possible causes.

---

<div class="post-metadata">

### Author: ![Nick2](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@Nick2](https://discourse.processing.org/u/Nick2)
#### Post date: [March 26, 2021, 8:07pm UTC](https://discourse.processing.org/t/problems-with-converting-a-string-to-a-float/28761/8 "2021-03-26T20:07:01Z")

</div>

Arduino code:

//

```auto
// FILE: DS18B20_two_sensors.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.0.1
// PURPOSE: demo with two sensors (on two pins)
//
// HISTORY:
// 0.0.1 = 2017-07-25 initial version

#include <OneWire.h>
#include <DS18B20.h>

// numbers chosen to match pin numbers..
#define ONE_WIRE_BUS2 2
#define ONE_WIRE_BUS3 3
#define ONE_WIRE_BUS4 4

OneWire oneWire2(ONE_WIRE_BUS2);
OneWire oneWire3(ONE_WIRE_BUS3);
OneWire oneWire4(ONE_WIRE_BUS4);

DS18B20 sen1(&oneWire2);
DS18B20 sen2(&oneWire3);
DS18B20 sen3(&oneWire4); 

void setup(void)
{
  Serial.begin(9600);
  Serial.println( __FILE__ );
  Serial.print("DS18B20 Library version: ");
  Serial.println(DS18B20_LIB_VERSION);

  sen1.begin();
  sen2.begin();
  sen3.begin();

  // different resolution shows nicely the async behavior
  sen1.setResolution(11);
  sen2.setResolution(11);
  sen3.setResolution(11);

  sen1.requestTemperatures();
  sen2.requestTemperatures();
  sen3.requestTemperatures();
}

void loop(void)
{
  // print the temperature when available and request a new reading
  if (sen1.isConversionComplete())
  {
    //Serial.print(" , ");
    Serial.print("sen1");
    Serial.print(",");
    Serial.println(sen1.getTempC(),1);//
    //Serial.print(","); 
    sen1.requestTemperatures();
    delay(500);

  }
  
  if (sen2.isConversionComplete())
  {
    //Serial.print(",");
    Serial.print("sen2");
    Serial.print(",");
    Serial.println(sen2.getTempC(),1);
    sen2.requestTemperatures();
    //Serial.print(" , ");
    delay(500);
  }
  
  if (sen3.isConversionComplete())
  {
   //Serial.print(" , ");
   Serial.print("sen3");
   Serial.print(",");
   Serial.println(sen3.getTempC(),1);
   //Serial.print(" , ");
   sen3.requestTemperatures();
   delay(500);  
   
  }
  delay(1000);
}

```

---

<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: [March 27, 2021, 1:21am UTC](https://discourse.processing.org/t/problems-with-converting-a-string-to-a-float/28761/9 "2021-03-27T01:21:58Z")

</div>

Hello,

I can assist if you edit your original code and format it as per:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

`:)`

---

<div class="post-metadata">

### Author: ![Nick2](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@Nick2](https://discourse.processing.org/u/Nick2)
#### Post date: [March 27, 2021, 5:51pm UTC](https://discourse.processing.org/t/problems-with-converting-a-string-to-a-float/28761/10 "2021-03-27T17:51:27Z")

</div>

Processing code:

```auto
import processing.serial.*;

Serial myPort;
int lf = 10;
float temp =0.1;
String s =null

void setup() {
myPort = new Serial(this, "COM5", 9600);
myPort.clear();
s = myPort.readStringUntil(lf);
}

void draw() {
while (myPort.available() > 0) {
s = myPort.readStringUntil(lf);

if (s != null) {
String[] list = split(s, ",");
 
String sen1 = list[0];

float temp = float(list[1]);

delay(600);
    }
   } 
  }

```

---

<div class="post-metadata">

### Author: ![Nick2](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@Nick2](https://discourse.processing.org/u/Nick2)
#### Post date: [March 27, 2021, 6:45pm UTC](https://discourse.processing.org/t/problems-with-converting-a-string-to-a-float/28761/11 "2021-03-27T18:45:55Z")

</div>

Do you mean the arduino code that I use?

Arduino code:

```auto
  //
// FILE: DS18B20_two_sensors.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.0.1
// PURPOSE: demo with two sensors (on two pins)
//
// HISTORY:
// 0.0.1 = 2017-07-25 initial version

#include <OneWire.h>
#include <DS18B20.h>

// numbers chosen to match pin numbers..
#define ONE_WIRE_BUS2 2
#define ONE_WIRE_BUS3 3
#define ONE_WIRE_BUS4 4

OneWire oneWire2(ONE_WIRE_BUS2);
OneWire oneWire3(ONE_WIRE_BUS3);
OneWire oneWire4(ONE_WIRE_BUS4);

DS18B20 sen1(&oneWire2);
DS18B20 sen2(&oneWire3);
DS18B20 sen3(&oneWire4); 

void setup(void)
{
  Serial.begin(9600);
  Serial.println( __FILE__ );
  Serial.print("DS18B20 Library version: ");
  Serial.println(DS18B20_LIB_VERSION);

  sen1.begin();
  sen2.begin();
  sen3.begin();

  // different resolution shows nicely the async behavior
  sen1.setResolution(11);
  sen2.setResolution(11);
  sen3.setResolution(11);

  sen1.requestTemperatures();
  sen2.requestTemperatures();
  sen3.requestTemperatures();
}

void loop(void)
{
  // print the temperature when available and request a new reading
  if (sen1.isConversionComplete())
  {
    //Serial.print(" , ");
    Serial.print("sen1");
    Serial.print(",");
    Serial.println(sen1.getTempC(),1);//
    //Serial.print(" , "); 
    sen1.requestTemperatures();
    //delay(400);

  }
  
  //if (sen2.isConversionComplete())
  //{
    //Serial.print(" , ");
    //Serial.print("sen2:\t");
    //Serial.print(" , ");
    //Serial.println(sen2.getTempC(),1);
    //sen2.requestTemperatures();
    //Serial.print(" , ");
    //delay(1000);
  //}
  
  //if (sen3.isConversionComplete())
  //{
   //Serial.print(" , ");
   //Serial.print("sen3:\t");
   //Serial.print(" , ");
   //Serial.println(sen3.getTempC(),1);
   //Serial.print(" , ");
   //sen3.requestTemperatures();
   //delay(1000);  
   
  //}
  delay(500);
}
   

```

---

<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: [March 27, 2021, 7:06pm UTC](https://discourse.processing.org/t/problems-with-converting-a-string-to-a-float/28761/12 "2021-03-27T19:06:35Z")

</div>

Hello,

Take a look at this:

> **Arduino**
>
> ```auto
> void setup(void)
> {
> Serial.begin(9600);
> Serial.println("DS18B20 Library version: ");
> }
> 
> void loop(void)
> {
> Serial.print("sen1");
> Serial.print(",");
> Serial.println(float (millis()));
> delay(1000);
> }
> 
> ```

> **Processing**
>
> ```auto
> import processing.serial.*;
> 
> Serial myPort;
> int lf = 10;
> float temp =0.1;
> String s = null;
> 
> void setup() 
> {
> // List all the available serial ports:
> printArray(Serial.list()); 
>   
> myPort = new Serial(this, "COM6", 9600);
> //myPort.clear();
> s = myPort.readStringUntil(lf);
> //println("s0:", s);
> }
> 
> void draw() 
> {
> while (myPort.available() > 0) 
> {
> s = myPort.readStringUntil(lf);
> //println("s1:", s);
> 
> if (s != null) 
> {
> println("s2:", s);
> String[] list = trim(split(s, ","));
> //String[] list = split(s, ",");
> //String sen1 = list[0];
> //println("sen1: ", sen1);
> //printArray(list);
>       
> if(list.length == 2)
> {
> printArray(list);
> float temp = float(list[1]);
> println("temp: ", temp);
> }
> //delay(600);
> }
> }
> }
> 
> ```

I wrote some test code on Arduino that I know the behavior of from the Arduino serial monitor.

I added some println() statements and other code to the Processing side to monitor incoming data.

trim() will removed the “linefeed” from the string; this may not be necessary if the float conversion can remove it. I still remove it in all code I write.

You can comment and uncomment as you desire to see what is going on when testing your code.

I also checked for an expected and valid array length before trying to read data from it.

My first pass at this with some tweaks to your code:

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

Also, I just worked through your code… _there are other ways to do this_.

I prefer to use:  
_[serialEvent() \ Language (API) \ Processing 3+](https://processing.org/reference/libraries/serial/serialEvent_.html)_

Please format your first post in this topic; it is messy.

`:)`

---

<div class="post-metadata">

### Author: ![Nick2](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@Nick2](https://discourse.processing.org/u/Nick2)
#### Post date: [April 3, 2021, 9:46am UTC](https://discourse.processing.org/t/problems-with-converting-a-string-to-a-float/28761/13 "2021-04-03T09:46:15Z")

</div>

Thank you very much! This worked!
