A simple data split , what am I doing wrong?

I am receiving a 2 value string from an arduino delimited by a space.

if(serialConnect)
{
  if (myPort.available() > 0) {
    IncomingSTR = myPort.readStringUntil(lf);
    //println (IncomingStr);
    if (IncomingSTR != null) {
      println (IncomingSTR);


      String [] list = split(IncomingSTR, ' ');
      if ( list.length >=2 )
      {
        int   Mach_ID= int (list [0]);
        int  LastUptime= int(list [1]);

println (Mach_ID+ "   and   " +LastUptime);
println (list [0]);
println (list [1]);

        //println ( Dev);
       // SaveToFile ();
      }
    }
  } // end of if port available

the int LastUptime always comes as a zero.
While the println (list [1]); is returning the correct integer from ardu.

So simple, I fee stupid, where is the mistake?
Thanks

        int  LastUptime= int(trim(list [1]));

Christof

Danke,

That worked. The processing documentation does not mention the trim under the Split reference.
How would someone know?

they go:

Name ### split()
Examples String men = “Chernenko,Andropov,Brezhnev”; String[] list = split(men, ‘,’); // list[0] is now “Chernenko”, list[1] is “Andropov”…

Maybe they should update the user guide…

Thanks again
mitch

1 Like

Haha, good point! :wink:

You should not have to use the split function there.

I think you have a whitespace character at the end of your string that is not a “space”. So when you split with the space character, the first value is correct but the second one is “value + whitespace”. And because of that you can’t convert it to an int and you first need to use the trim() function to get rid of that whitespace.

So no need to update the documentation here.

1 Like