# Question about trims and integers

**URL:** <https://discourse.processing.org/t/question-about-trims-and-integers/18255>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [February 29, 2020, 9:50pm UTC](https://discourse.processing.org/t/question-about-trims-and-integers/18255 "2020-02-29T21:50:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![SouSageYa](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sousageya/32/9819_2.png) [@SouSageYa](https://discourse.processing.org/u/SouSageYa)\
**Post date:** [February 29, 2020, 9:50pm UTC](https://discourse.processing.org/t/question-about-trims-and-integers/18255/1 "2020-02-29T21:50:39Z")

</div>

```
String[] a = split(receivedString, ','); 
println(a[0]); 
println(a[1]); 
println(a[2]); 
println(a[3]); 
println(a[4]); 
println(a[5]); 
println(a[6]); 

int val1 = Integer.parseInt(a[0].trim()); 
int val2 = Integer.parseInt(a[1].trim()); 
int val3 = Integer.parseInt(a[2].trim()); 
int val4 = Integer.parseInt(a[3].trim());     
int val5 = Integer.parseInt(a[4].trim()); 
int val6 = Integer.parseInt(a[5].trim());     
int val7 = Integer.parseInt(a[6].trim());
XChart.push("incoming",val1);
YChart.push("incoming",val2);
ZChart.push("incoming",val3);
GXChart.push("incoming",val4);
GYChart.push("incoming",val5);
GZChart.push("incoming",val6);
TempChart.push("incoming",val7);

```

As you can see I have trims but when I try to trim decimally numbers I can’t. I tried to round them but using two local variables is impossible please help. I’m also editing in Java(ProcessingIDE).

---

<div class="post-metadata">

**Author:** ![commie630](https://avatars.discourse-cdn.com/v4/letter/c/ee7513/32.png) [@commie630](https://discourse.processing.org/u/commie630)\
**Post date:** [February 29, 2020, 9:53pm UTC](https://discourse.processing.org/t/question-about-trims-and-integers/18255/2 "2020-02-29T21:53:53Z")

</div>

Please format the start of your code correctly… I believe the Java trim method does not belong specifically to processing so maybe try your question on Stack Overflow

---

<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:** [February 29, 2020, 11:20pm UTC](https://discourse.processing.org/t/question-about-trims-and-integers/18255/3 "2020-02-29T23:20:54Z")

</div>

If it is a “string float” then parse it to a “number float” and then you can cast it to a “number int” (you will lose decimal points).

```auto
String a = " 1.2345 \n"; //Added spaces and a \n so there was something to trim.
println(float(a.trim()));
println(float(trim(a)));

println(int(float(a.trim())));
println(int(float(trim(a))));

```

Same as:

```auto
String a = " 1.2345 \n";

println(parseFloat(a.trim()));
println(parseFloat(trim(a)));

println(parseInt(PApplet.parseFloat(a.trim())));
println(parseInt(PApplet.parseFloat(trim(a))));

```

[https://processing.org/reference/trim\_.html](https://processing.org/reference/trim_.html)

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

`:)`

---

<div class="post-metadata">

**Author:** ![SouSageYa](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sousageya/32/9819_2.png) [@SouSageYa](https://discourse.processing.org/u/SouSageYa)\
**Post date:** [March 1, 2020, 8:00am UTC](https://discourse.processing.org/t/question-about-trims-and-integers/18255/4 "2020-03-01T08:00:33Z")

</div>

I tried the code that you wrote thanks for trying but it didn’t help I have to send multiple values from arduino to Processing and that code doesn’t work when I try that. If you can edit the code I owe you

By the way data’s are like: -0.2132143214321,109.123214,-30.3214321

---

<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 1, 2020, 11:48am UTC](https://discourse.processing.org/t/question-about-trims-and-integers/18255/5 "2020-03-01T11:48:18Z")

</div>

> [@SouSageYa](#):
>
> If you can edit the code I owe you

This is your project; I will only provide guidance.

I modified your String to have a line feed at the end; I assume you are using println() on the Arduino side and it will send this and trim() will remove that.

You are trying to parse a float into an int and you will get a:

> NumberFormatException: For input string: “-0.2132143214321”

You must parse your float into a float.

I also recommend that you trim the received String; it is not necessary for each element of the array unless you have whitespace characters in there.

> **[trim() / Reference](https://processing.org/reference/trim_.html)**
>
> Removes whitespace characters from the beginning and end of a String. In addition to standard whitespace characters such as space, carriage return, and tab, this function also removes the Unicode "nbs…

Code to show how I work through a problem:

```auto
String receivedString = "-0.2132143214321,109.123214,-30.3214321\n";

String[] a = split(receivedString, ','); // Try trimming string here
printArray(a);

println(a[2]); 

// This will not work
int val1 = Integer.parseInt(a[2].trim()); // You should be parsing into a float here!
println (val1);

// You should be parsing into a float
// You can modify above and add here

```

---

<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:** [March 1, 2020, 1:16pm UTC](https://discourse.processing.org/t/question-about-trims-and-integers/18255/6 "2020-03-01T13:16:12Z")

</div>

> [@SouSageYa](#):
>
> I have to send multiple values from Arduino to Processing…

> [@\[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:** ![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 1, 2020, 4:51pm UTC](https://discourse.processing.org/t/question-about-trims-and-integers/18255/7 "2020-03-01T16:51:54Z")

</div>

It seems to me that you are trying to read a “String representing a floating-point-number (fpn)” as an “int” in one step. That is never going to work well, you need to convert the string to a float _then_ convert the float to an int by rounding like this

```auto
String receivedString = "-0.2132143214321, 109.123214, -30.3214321, -0.96743, 3.14159";
println("Inpit:\n" + receivedString);
String[] a = split(receivedString, ','); // Get an array of fpn(s) as Strings
println("Array Values:");
float[] f = new float[a.length]; 
int[] v = new int[a.length];
for(int i = 0; i < a.length; i++){
  f[i] = Float.parseFloat(a[i].trim()); // convert String to float
  v[i] = round((float)f[i]); // convert float to int
  println(f[i] + "\t\t" + v[i]);
}

```

---

<div class="post-metadata">

**Author:** ![SouSageYa](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sousageya/32/9819_2.png) [@SouSageYa](https://discourse.processing.org/u/SouSageYa)\
**Post date:** [March 12, 2020, 5:54pm UTC](https://discourse.processing.org/t/question-about-trims-and-integers/18255/8 "2020-03-12T17:54:58Z")

</div>

Thank you for the solution of the trimming but it turns out there is something else with this code as you can see I created values as Val1 for example and I defined them as “Integer.parseInt(a[6].trim());” I have to have the numbers like that so I can push them on chart

---

<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 13, 2020, 10:31am UTC](https://discourse.processing.org/t/question-about-trims-and-integers/18255/9 "2020-03-13T10:31:33Z")

</div>

My code produces an array of ints which can be added to a chart.

`v[]` _is an array of integers_

---

<div class="post-metadata">

**Author:** ![SouSageYa](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sousageya/32/9819_2.png) [@SouSageYa](https://discourse.processing.org/u/SouSageYa)\
**Post date:** [March 22, 2020, 3:53pm UTC](https://discourse.processing.org/t/question-about-trims-and-integers/18255/10 "2020-03-22T15:53:31Z")

</div>

Thank you for all you are the best. Much love from Turkey
