Recieving data from arduino

edited July 2014 in Arduino

I am wondering if there's a way to use serial in processing to read both strings and integers and separate the strings from the integers.

Answers

  • edited July 2014 Answer ✓

    Discriminating general String objects from meaningful numerical values is a question of parsing.
    Java's got a method called Integer.parseInt(""). When it fails, it throws a NumberFormatException.
    Thus we know the argument wasn't a valid numerical value, and gotta be treated as a String:

    http://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#parseInt-java.lang.String-

    Check out this example I've come up w/: <):)

    // forum.processing.org/two/discussion/6380/recieving-data-from-arduino
    
    final IntList ints = new IntList();
    final StringList strs = new StringList();
    
    final String[] elems = {
      "-5", "010", "1e3", "100.", "500", "Test"
    };
    
    for (String e: elems)  try {
      ints.append(Integer.parseInt(e));
    }
    catch (NumberFormatException nfe) {
      strs.append(e);
    }
    
    println(ints);
    println(strs);
    
    exit();
    
Sign In or Register to comment.