Click to See Complete Forum and Search --> : Nested if statment


Hind
02-26-2006, 11:31 AM
In the following code:
public int getType(String type) {
if(type == "Genaralization")
return 7;
else
if(type == "Realization")
return 2;
else
if(type == "Dependency")
return 3;
else
if(type == "aggregate")
return 4;
else
if(type == "composite")
return 5;
else
if(type == "none")
return 6;

return 1;
}
It OK for "Genaralization" and "Realization", if the type is not of these two it returns 1. I don't Know where is the problem.

please if there is a way better than this nested if let me Know.

Khalid Ali
02-26-2006, 01:55 PM
you could use switch statements in this fashon as well. However my guess is ur comparison is wrong instead of == operators you want to use type.equals("type")

Hind
02-26-2006, 02:30 PM
Can I use switch statment with Strings??

Khalid Ali
02-26-2006, 02:51 PM
lol...sorry I wasn't thinking....nope switch statement can not be used with strings...however comparison part of my response is still correct

Hind
02-26-2006, 03:25 PM
Thanks, it works as you told me