I'm missing something, not expected output from array.
Ok, I'm writing a little rpg class to help expand my knowledge on the inner workings of java. Starting with arrays and strings, seeing as I know how to use them in their basic form, but I don't know how they really tick.
I'm pretty sure the problem is somewhere in the class that is saving the hero's inventory to the object. However I'm not sure why.
Hero.java
Code:
public void setHeroInventory(String addItem, String remItem)
{
if(!addItem.equals(null))
{
for(int i=0; i<heroInventory.length;i++);
{
if(!(heroInventory[i] == null))
{
heroInventory[i] = addItem;
}
}
}
else
{
for(int i=0; i<heroInventory.length; i++)
{
if(heroInventory[i].equals(remItem))
{
heroInventory[i] = null;
}
}
}
}
HeroTestDrive.java
Code:
public static void main(String[] args)
{
Hero warrior = new Hero();
String[] heroInventory = new String[20];
for(int i = 0; i<heroInventory.length; i++)
{
heroInventory[i] = null;
}
warrior.setHeroInventory("Dagger",null);
for(int x=0; x<heroInventory;x++)
{
heroInventory[x] = warrior.getHeroInventory(x);
if (!(heroInventory[x] == null))
{
System.out.println("Warrior's Inventory Slot["+x+"]: "+heroIventory[x]);
}
}
System.out.println("Warrior Test Complete.");
}
Output
Code:
Warrior Test Complete.
Its not very complex, but I'm not sure why it doesn't save Dagger to warrior.setHeroInventory[0]. I'm sure I'm missing something very simple, but does anyone have any ideas?