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 parse a "long" int
Page Index Toggle Pages: 1
how to parse a "long" int? (Read 829 times)
how to parse a "long" int?
Sep 26th, 2007, 3:14pm
 
My sketch needs to read in database keys that are "long" ints (64-bits).

Processing can convert "String" to "int", but seems to have a problem converting "String" to "long". I am hoping this is a small oversight that can be fixed in the next update to Processing.

Here is some code that illustrates the problem:
Code:
void setup() {
int x = int("17");
long y = long("27"); // this causes the error message
// workaround
long z = java.lang.Long.parseLong("27"); // this looks so ugly!
}


The compile-time error message is:
Code:
Semantic Error: No accessible method with signature "parseLong(java.lang.String)" was found in type "processing.core.PApplet".


The JAVA documentation tells me there is a "parseLong" method available in java.lang.Long, but Processing doesn't seem to know about it, since the error message is referring to java.lang.String.  There isn't a "parseInt" in java.lang.String either, but Processing knows how to handle that conversion, ...

It would be great if Processing could be "educated" about "long" for the next update, since many large databases (which we might like to visualize using Processing) have keys that are larger than 32-bit "int"s.

thanks,
 djones
Re: how to parse a "long" int?
Reply #1 - Sep 26th, 2007, 3:33pm
 
You don't need the java.lang. bit in front of Long.

Code:
long a=Long.parseLong("1234567890123");
String b=(new Long(a)).toString();
println(b);


Also, you're misreading the error, it's not saying there's no parseLong in java.lang.String class, it's saying there's no method parseLong which takes a java.lang.String argument, in the processing.core.PApplet class.

To get your nice long("..."); syntax, you could just write your onw little function to do it:
Code:
long mylong(String a)
{
return Long.parseLong(a);
}

void setup()
{
long a=mylong("12313123123");
}
Re: how to parse a "long" int?
Reply #2 - Sep 27th, 2007, 12:05am
 
put another way, the confusion is that the syntax:

int("1234");

is converted to

parseInt("1234");

and "parseInt()" is a function in PApplet. it's a shortcut for typing "Integer.parseInt()", which is the way you'd do it in java. so these all do (basically) the same thing:

int("1234");
parseInt("1234");
Integer.parseInt("1234");

when you're trying to do the same thing with long, your code that says

long("1234");

is getting converted to:

parseLong("1234");

which is not a built-in function the way parseInt() is. so instead you have to use the java-style method, Long.parseLong() to do what you want.
Re: how to parse a "long" int?
Reply #3 - Sep 27th, 2007, 5:04pm
 
Thanks John and Ben for explaining the details of Long.parseLong(s), etc.

I notice that while the syntax x=int("7") and y=float("3.14") are in Processing, neither x=long("7") nor double("3.14") are supported.

I guess it comes down to an aesthetic or philosophical choice whether to provide the same kind of "shortcut" for long and double that are already provided for int and float.

If it was up to me, I'd advocate for including long() and double() shortcuts, since I much prefer being able to stay within Processing and being insulated from Java as much as possible.  (and I think my students would as well).

-- djones

Page Index Toggle Pages: 1