Parse string to long

edited August 2015 in Programming Questions

Hi there

I am reading a string from serial port (from Arduino) and need to convert it to long. My code throws an exception every time - and catches it. Isn't the Long.parseLong(String) the way to parse string to long? :

String val; //global variable

[..]
and in draw():

 if ( myPort.available() > 0) 
  {  // If data is available,
  val = myPort.readStringUntil('\n');         // read it and store it in val
  println("Val=" + val);
  myPort.clear();
  } 

  // While Java's parseLong() throws an Exception:
  try {
   println("long l=" + Long.parseLong(val));
  } catch (NumberFormatException e) {
   println(val);
   println("can't be converted to a number!");
   }

  exit();

Many thanks in advance

Answers

Sign In or Register to comment.