Please help me TT


String a = "1,235,345,234,123,234,345.\n"; //channel(0,1,2),value,value,value,value,value,value.\n
String[] temp = split(a,',');
String CH = temp[0].replace("\n","");

//int value1 = int(temp[1]);
//int value2 = int(temp[2]);
//int value3 = int(temp[3]);
//int value4 = int(temp[4]);
//int value5 = int(temp[5]);
//int value6 = int(temp[6].replace(".",""));

if (CH == "0") {
  println("CH is 0");
}
else if (CH == "1") {
  println("CH is 1");
}
else if (CH == "2") {
  println("CH is 2");
}
println("completed"); 

I made this code, and I expected
“CH is 1”
“completed”

but it shows only
“completed”

so I tryed this on https://hello.processing.org/editor/#editor
and it worked well
please let me know why my processing does not show “CH is 1”

I’m not good at English, sorry

1 Like
String a = "1,235,345,234,123,234,345.\n";
String[] temp = split(a,',');
String CH = temp[0].replace("\n","");

if      (CH.equals("0")) println("CH is 0");
else if (CH.equals("1")) println("CH is 1");
else if (CH.equals("2")) println("CH is 2");
println("completed");

shows

CH is 1
completed

pls see
https://processing.org/reference/String_equals_.html

and understand that the way you posted your code
makes it unusable.

also the 3d line code is wrong, as it looks for temp[0] ( “1” )
but the “\n” is in temp[6]
please use a print after each operation to understand what it does.

1 Like

It works very well, thank you

I’m so sorry but problem occured again
My arduino sends
0,value,value,value,value,value,value.
1,value,value,value,value,value,value.
2,value,value,value,value,value,value.
0,value,value,value,value,value,value.
.
.
.
by serial

I thought “\n” is in temp[0]
So i placed replace("\n","") to temp[0]
But it only prints
“completed”
“completed”
“completed”
.
.
.

The first three lines of your code do this:

String a = "1,235,345,234,123,234,345.\n";

Makes a string with a linebreak (\n) at the end:

1,235,345,234,123,234,345.
 

String[] temp = split(a,',');

Turns it into an array:

0: 1
1: 235
2: 345
3: 234
4: 123
5: 234
6:

 

Notice that last one still has a period and a linebreak at the end.

String CH = temp[0].replace("\n","");

Removes any linebreaks from [0]. There aren’t any, so it does nothing.

[6] still has a period and a linebreak at the end.