I have a program that takes characters from the buffer to form a string.
My program is:
while (i < 5) {
ch = inStream.read();
buffer.append((char)ch);
i++;
}
data = buffer.toString();
For my program above, I can only take 5 characters to form a string. What I want to do is have a variable length of string. The length of the string will depend on the detection of <CR>. The serial device I am using always terminates the data by <CR>. By doing it, my string can have any length.
Problem solved. Here’s what I tried and worked perfectly – assuming 300 is my max and i want to detect /n as well.
My code:
while (i < 300) {
ch = inStream.read();
if ((ch=='\r')||(ch=='\n')) {
i = 300;
}
buffer.append((char)ch);
i++;
}
data = buffer.toString();
Assuming you mean with
<CR>“Carriage Return”, you should useBufferedReader: