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.
Page Index Toggle Pages: 1
check datatype (Read 1525 times)
check datatype
May 6th, 2010, 6:48am
 
Hi All,

I'd like to check the datatype of a variable in Processing. How is this done?
I want to know because I'm checking this expression:
if(temp[5].startsWith("-") == true)
in one instance temp[5] holds -6.6 but it still doesn't evaluate to true. Does anyone know why?

Thanks a lot, best, Danielle.
Re: check datatype
Reply #1 - May 6th, 2010, 7:22am
 
If it's a primitive type (float/int/byte/boolean) then I'm not sure how you could not know what it is... if it's an Object type, then you can use instanceof

e.g.
if(temp[5] instanceof String)
{
 //do something...
}

Re: check datatype
Reply #2 - May 6th, 2010, 7:25am
 
The data type is actually what you declare when you define your variable.
If you wrote:
String[] temp;
then you can do what you show, and it should work (I prefer to write: if (temp[5].startsWith("-")) as booleans are self-sufficient in tests).
If you wrote:
float[] temp;
then your code isn't legal, but you can do:
if (temp[5] < 0)
Re: check datatype
Reply #3 - May 6th, 2010, 7:39am
 
Thanks both for your replies!
Yes, I indeed defined the temp variable as a String.
I've discovered that I made a little mistake  Embarrassed and that the actual string in temp[5] is '-6.6' and not just -6.6. As I just want to remove the - sign now try to remove it by first checking if it's there using charAt like this:
if(temp[5].charAt(1) == "-")
Now I get the error:'Incompatible oparand types char and String'...
Does any of you know how I can check for a character and then remove it.

Thanks again, d
Re: check datatype
Reply #4 - May 6th, 2010, 8:07am
 
If you are doing character comparisons, you should enclose any literal characters in single quotes not double quotes (which are used for Strings rather than characters). So in your example, you would use

if(temp[5].charAt(1) == '-')

However, I do wonder if you are approaching the problem in the right way. Are you trying to treat positive and negative numbers in the same way? In which case it may be better to convert the entire string into a number and then take the abs() value of the number.

Alternatively you could do a simple search and replace of any '-' characters in your strings with empty strings, e.g.

String positiveString = temp[5].replace("-","");

or

String positiveString = temp[5].substring(1,temp[0].length()-1).replace("-","");

if you want to remove the single quotes too.

Jo.
Re: check datatype
Reply #5 - May 6th, 2010, 10:31am
 
Hi Jo,

Thanks, that was very helpful. You say, convert string to a number. That's what I tried with this code:
// temp[7] holds: '123' for example
     String clean_o3 = temp[7].replace("'", ""); // remove ''
     int O3 = int(clean_o3); // clean_o3 holds 123 when printed
     println(O3); // outputs 0

Why does the O3 not hold 123 as an int but is it converted to 0?
Thanks!
Re: check datatype
Reply #6 - May 6th, 2010, 11:57am
 
Perhaps there is more (or less) than you show...
Code:
void setup()
{
String temp = "'123'";
String clean_o3 = temp.replace("'", ""); // remove ''
println(clean_o3);
int O3 = int(clean_o3); // clean_o3 holds 123 when printed
println(O3); // outputs 0
}

I get 123 twice.
Re: check datatype
Reply #7 - May 7th, 2010, 1:39am
 
Hi PhiLho,

Thanks for the tip. There was a whitespace I overlooked in the string. I've added a trim function, now it works.

Best, d.
Page Index Toggle Pages: 1