We are about to switch to a new forum software. Until then we have removed the registration on this forum.
WHY pls help me :( iam very said
void setup(){
println(getTileID("grass"));
}
int getTileID(String name) {
int id = 0;
if (name.equals("stone")) {
id = 41;
} else if (name.equals("grass")) {
id = 10;
} else if (name.equals("wood")) {
id = 23;
} else {
id = 0;
}
return id;
}
Answers
Prints
10
for me. And id 10 is grass. So... what's the problem?Works fine for me. Prints:
yes it is my mystake on my game i have null value on String :D
If 1 of the strings you wanna compare is a literal
""
, use that to invoke the method equals(), for the obvious reason a String literal is never anull
: *-:)else if ( "grass".equals(name) ) { return 10; }
A
switch () / case:
block is another excellent option:https://Processing.org/reference/switch.html
An IntDict container and even enums: :-bd
https://Processing.org/reference/IntDict.html
https://Docs.Oracle.com/javase/tutorial/java/javaOO/enum.html
you can check for null:
int getTileID(String name) {
if(name==null) return -1; // error / unknown
int id=0;
if ......