Trying to extract a float from a string with letters, numbers, and chars.
in
Programming Questions
•
1 year ago
Hello folks, I have a fairly simple problem here. I have a robot project and I'll simplify a bit but basically the communication syntax between robot and computer looks a bit like this "<0.1234JX>". I realize now that its not the best way but it worked well when the robot was in its infant stages, and rather than rewrite every location where they communicate, i really just need to pull out the 0.1234 from that string. the < and > will always be at the beginning and end. Im looking at numbers in about the range of 1000 down to 0.0001, and including negatives. The Arduino on the robot can parse this out easily with the atof(str) function, but i can't seem to find a processing equivalent. I dont want to use a for() loop for parsing, if I can avoid it. I have tried using Float.parseFloat(trim(clientindata)); and similar, but I get either a NaN or a communication error. Any ideas? Greatly appreciated!
Here is a section of the code:
- try {
- if (thisClient != null) {
- String clientindata = thisClient.readStringUntil('\n');
- if (clientindata != null) {
- //system can read data coming from client here
- indata = Float.parseFloat(trim(clientindata));
- if (match(clientindata, "JZ") != null){
- camHjoyset = indata;
- println("joyyz=" + indata);
- }
- if (match(clientindata, "JS") != null){
- camVjoyset = indata;
- println("joyys=" + indata);
- }
- com4.write(trim(clientindata));//forward client data to Arduino
- println(thisClient.ip() + " : " + clientindata);
- }
- }
- }catch(Exception e) {
- println("Client port error");
1