We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › how to check a string can be converted to intger
Page Index Toggle Pages: 1
how to check a string can be converted to intger (Read 647 times)
how to check a string can be converted to intger
Oct 4th, 2006, 5:12pm
 
hello there i would just like to know how to get processing to check a piece string being feed into can be converted to an integer.

i am using thwe following sort of code to do the converting where buff is string being feed in realtime and val is an integer output.

val = Integer.parseInt(buff);

right now processing crashes when string that is not an integer(random char) is received.

the senario is that i am reading numbers from the serial port and every now and again a random char comes in and then the program crashes because of the char or string cannot be converted

any help is appreciated also if you want the full serial code just ask.
Re: how to check a string can be converted to intg
Reply #1 - Oct 4th, 2006, 5:39pm
 
check out the try-catch-clause:

Code:
String buff = "1";
int val;

try {
val = Integer.parseInt(buff);
println(val);
}
catch (NumberFormatException e) {
println("couldn t parse string. " + e);
}
Re: how to check a string can be converted to intg
Reply #2 - Oct 4th, 2006, 6:51pm
 
use parseInt, and add a second parameter to it for the number that you want to be returned if it's not a number:

int blah = parseInt("1234", 9999);

blah will be 9999. but for this one:

int blah = parseInt("potato", 9999);

blah should return 9999.
Page Index Toggle Pages: 1