FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Integration
(Moderators: fry, REAS)
   param() string in if expression
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: param() string in if expression  (Read 776 times)
Prinds

Email
param() string in if expression
« on: Jan 24th, 2005, 9:45pm »

Hi everyone..
 
I have run into a problem with the param() function when using the returned string in an if expression...
 
the problem is displayed in a little sketch here.. http://praktiskegris.1go.dk/processing/stringbug/ ...
 
the general idea is that i want to get a parameter from the <applet> tag, which I then output as a string using text()... next I compare the string in the if expression, but eventhough the string appears the same, the expression still returns false resulting in the wrong text being outputted...
 
the html code as follows...
<applet code="stringtest" archive="stringtest.jar" width=100 height=100>
<param name="param1" id="param1" value="htmlstring">
</applet>
 
and the processing code...
String node = "defaultvalue";
BFont fontA = loadFont("Futura-Book.vlw.gz");
 
 
textFont(fontA, 24);
 
node = param("param1");
 
text(node,2,30);
 
if(node=="htmlstring"){
text("correct",2,60);
}else{
text("wrong",2,60);
}
 
I have worked around this problem using integers, but would really rather make the string solution work, so if anyone knows, what to do i would apreciate it, since this goes somewhat beyond my reason...
 
thanks,
 
Prinds
 
fjen

WWW
Re: param() string in if expression
« Reply #1 on: Jan 25th, 2005, 12:58am »

no that's correct.
 
see tom's post on that topic
 
compare like this:
if (node.equals("htmlstring")) { ...
 
/F
 
Prinds

Email
Re: param() string in if expression
« Reply #2 on: Jan 25th, 2005, 1:28am »

thank you very much... though very vague, I see some degree of sense in that, but more importantly I see that it will work, and that was the main point of this post...
 
but should somebody desire to share their wisdom on strings and comparison, I would like to hear it... just for the fun of it..
 
Prinds
 
fjen

WWW
Re: param() string in if expression
« Reply #3 on: Jan 26th, 2005, 4:12am »

the == operator checks wether two objects are exactly the same object.
 
Code:

String a = "abc";
String b = "abc";

will refer to the one same object (created by java for all instances of "abc" in the code while compiling)
 
-- but --
 
Code:
String a = new String("abc");
String b = "abc";
String c = loadStrings("abc.txt");

will all be different objects ... so == will fail.
 
a.equals(String b) will compare the contents (value) of the Strings rather than the string-objects themselfs.
 
hope that's clear and correct .. tom?
/F
 
TomC

WWW
Re: param() string in if expression
« Reply #4 on: Jan 26th, 2005, 11:59am »

Yes, spot on.
 
The == operator checks identity. Two variables are identical only if they refer to the same Object, or in the case of primitives (int, float, etc.) if they are the same value.
 
Perhaps the String reference should contain some of these hints.
http://processing.org/reference/String.html
 
(My FAQ is delayed, but it's on the way, honest!)
« Last Edit: Jan 26th, 2005, 12:02pm by TomC »  
Pages: 1 

« Previous topic | Next topic »