OK, so I'm making a video game and I have a simple inventory code. This is the inventory class:
public class Inventory {
static int slot[] = new int[17];
static boolean[] taken = new boolean[17];
static int item[] = new int[17];
static int count[] = new int[17];
public static void addToSlot(int i, int j)
{
String itemNumString = Integer.toString(i);
for(int k = 1;k<=16;)
{
String slotNumString = Integer.toString(k);
if(taken[k] == true)
{
System.out.println("Slot ".concat(slotNumString) + " is already taken");
k++;
} else {
System.out.println("Item of the ID ".concat(itemNumString) + " has been added to the slot of the ID ".concat(slotNumString));
item[k] = i;
count[k]++;
taken[k] = true;
}
}
}
}
What it does is check every slot, and if it's taken continue the loop, if not stop the loop & add the item to that slot.
But the console outputs:
Item of the ID 1 has been added to the slot of the ID 1
Slot 1 is already taken
Item of the ID 1 has been added to the slot of the ID 2
Slot 2 is already taken
Item of the ID 1 has been added to the slot of the ID 3
Slot 3 is already taken
Item of the ID 1 has been added to the slot of the ID 4
Slot 4 is already taken
Item of the ID 1 has been added to the slot of the ID 5
Slot 5 is already taken
Item of the ID 1 has been added to the slot of the ID 6
Slot 6 is already taken
Item of the ID 1 has been added to the slot of the ID 7
Slot 7 is already taken
Item of the ID 1 has been added to the slot of the ID 8
Slot 8 is already taken
Item of the ID 1 has been added to the slot of the ID 9
Slot 9 is already taken
Item of the ID 1 has been added to the slot of the ID 10
Slot 10 is already taken
Item of the ID 1 has been added to the slot of the ID 11
Slot 11 is already taken
Item of the ID 1 has been added to the slot of the ID 12
Slot 12 is already taken
Item of the ID 1 has been added to the slot of the ID 13
Slot 13 is already taken
Item of the ID 1 has been added to the slot of the ID 14
Slot 14 is already taken
Item of the ID 1 has been added to the slot of the ID 15
Slot 15 is already taken
Item of the ID 1 has been added to the slot of the ID 16
Slot 16 is already taken
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 17
at com.torradin.inventory.Inventory.addToSlot(Inventory.java:15)
at com.torradin.core.KeyEvents.getKeyEvents(KeyEvents.java:33)
at com.torradin.core.Main.<init>(Main.java:106)
at com.torradin.core.Main.main(Main.java:266)
Any help would be appreciated! :)