java - Checking enum value index -
i having issue while checking index of enums value, have enum varargs integer, if integer greater 1 , replaceable item, when next index increment one. but, when there not integer after increment, game crashes, i've been trying come solution past half hour or so, can't seem check if last index within enum. know localized question need help.
the code:
private int getnextid(food food, int id) { if (food.gettype().equals(replace)) { (int = 0; < food.getids().length; i++) { if (food.getids()[i] == id) { return food.getids()[i + 1]; } } } return -1; }
enumeration:
private enum food { shrimp(remove, 3, 315), lobster(remove, 12, 379), manta_ray(remove, 22, 391), monkfish(remove, 16, 7946), mackrel(remove, 6, 355), salmon(remove, 9, 329), sea_turtle(remove, 22, 397), shark(remove, 20, 385), swordfish(remove, 14, 373), trout(remove, 7, 333), tuna(remove, 10, 361), tuna_potato(remove, 22, 7060), cake(replace, 4, 1891, 1893, 1895), chocolate_cake(replace, 5, 1897, 1899, 1901), plain_pizza(replace, 7, 2289, 2291), meat_pizza(replace, 8, 2293, 2295), anchovy_pizza(replace, 9, 2297, 2299), pineapple_pizza(replace, 11, 2301, 2303), apple_pie(replace, 7, 2323, 2335), redberry_pie(replace, 5, 2325, 2333), meat_pie(replace, 6, 2327, 2331); private final consumabletype type; private final int healamount; private final int[] ids; private food(consumabletype type, int healamount, int... ids) { this.type = type; this.ids = ids; this.healamount = healamount; } public consumabletype gettype() { return type; } public int gethealamount() { return healamount; } public int[] getids() { return ids; } public string getname() { return name().tolowercase().replaceall("_", " "); } }
oh , note, have tried checking if it's -1 or 0, said can't seem figure out. in advanced.
are trying this?
private int getnextid(food food, int id) { if (food.gettype().equals(replace)) { int n = food.getids().length; (int = 0; < n; i++) { if (food.getids()[i] == id && (i+1) < n) { return food.getids()[i + 1]; } } } return -1; }
in above method, next id returned if exists, otherwise -1 returned.
Comments
Post a Comment