Trying to implement a switch case statement for an enumeration that lives in another class. i have cleaned the useless code out of the way for you all. My problem occurs at the Second code segment at the second line.
My current guess is it doesn't like the casting to byte in the enumeration because it is trying to use that as the index. If this is the case, how do i go about getting the byte to an index to be tested? I should mention i'v used this technique once before but with standard integer casting. such that "value(1)," for instance. So i thought i understood the concept of how this is working? but now i'm second guessing myself and wound up stuck. i have tried to google the snot out of this problem but i keep coming up short.
Is a switch case possible on byte castings of an enumeration???, if so, can someone please help with enlightening my java skills, or please point me to a better method of testing my byte. preferably something other than if else statements? i just thought it looked neater with the switch statement.
EDIT: So i appear to understand the problem of outofbounds, but that doesn't answer the question of how do i go about fixing the outofbounds to still work with the byte enumeration that i currently have?? other than making a specific if statement for the -15?
public class OtherClass {
public enum Ack_Nak{
ACK((byte)0x01),
NAK((byte)0x00),
NAK_SIZE((byte)0x51),
NAK_CRC((byte)0xCC),
NAK_DATA((byte)0xD8),
NAK_FILE((byte)0xF1);
private final byte i;
Ack_Nak(final byte i) {
this.i = i;
}
public byte getByte() {
return i;
}
}
}
Here is the Switch case i am trying to accomplish. the error i get is "java.lang.ArrayIndexOutOfBoundsException: length=6; index=-15"
byte ack_nak = anotherClass.getDataByte();
OtherClass.Ack_Nak Ack_Naks = OtherClass.Ack_Nak.values()[ack_nak]; // Problem Here
switch (Ack_Naks) {
case ACK:
//do something here
break;
case NAK:
case NAK_CRC:
case NAK_DATA:
case NAK_FILE:
case NAK_SIZE:
Toast.makeText(MainActivity.this,Ack_Naks.name() , Toast.LENGTH_SHORT).show(); //show failure
//act on the failure here
break;
default: //just incase
break;
}