More elegant syntax for logical operators for equals() method in Java? -
More elegant syntax for logical operators for equals() method in Java? -
i have code so:
if (input.equals("north") || input.equals("n")) { direction = cardinals.north; }
is there way utilize less verbose syntax this, along lines of:
if (input.equals("north" || "n") { direction = cardinals.north; }
i know doesn't work, hope there equivalent?
why not using enum directly:
public enum cardinals { north("n", "north"), south("s", "south"), east("e", "east"), west("w", "west"), error("", ""); private final list<string> matchingstrings; cardinals(string... matchingstringsar) { this.matchingstrings = arrays.aslist(matchingstringsar); } public static cardinals formstr(string str){ (cardinals cardinals : cardinals.values()){ if (cardinals.matchingstrings.contains(str)){ homecoming cardinals; } } homecoming error; } } // ..... cardinals direction = cardinals.formstr("n");
java syntax equals
Comments
Post a Comment