Question about trims and integers

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).

3 Likes

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

2 Likes

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).

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:

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

:)

3 Likes

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

1 Like

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.

Code to show how I work through a problem:

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
3 Likes
2 Likes

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

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]);
}
3 Likes

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

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

v[] is an array of integers

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