So I'm using the controlP5 library in processing, and I'm trying to do an if statement for the text entered in the text field, but for whatever reason it will not say it's equal. I've checked if it's a string, and that it's the correct string in other ways (instanceof and printing the textfield value), but it still will not confirm it's the same and I have no idea why.
import controlP5.*;
ControlP5 cp5;
void setup(){
size(500,500);
cp5 = new ControlP5(this);
PFont font = createFont("arial",20);
cp5.addTextfield("Input")
.setPosition(20,100)
.setFont(font)
.setFocus(true)
;
cp5.addBang("Enter")
.setPosition(20,150)
.setSize(80,40)
.getCaptionLabel().align(ControlP5.CENTER,ControlP5.CENTER)
;
}
void draw(){
background(0);
rectMode(CENTER);
rect(width/2,height/2,50,50);
}
public void Enter(){
String tempAns = cp5.get(Textfield.class,"Input").getText();
String stringcheck = "head";
if(tempAns instanceof String){
println("it is a string");
}
println(tempAns);
println(stringcheck +" check");
if(tempAns == "yes"||tempAns == stringcheck){
println("it works");
}
}
In Java (which Processing is based on), you must use
str1.equals(str2);
, notstr1 == str2
. See here for an explanation: Java String.equals versus ==