Results 1 to 4 of 4
- 04-19-2011, 06:11 PM #1
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
HeroTestDrive.javaJava 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; } } } }
OutputJava 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."); }
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?Java Code:Warrior Test Complete.
Last edited by Dark; 04-19-2011 at 06:15 PM.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 04-19-2011, 06:16 PM #2
You initialize your whole heroInventory array to be null. Then when you add an item, you only add it if an index is NOT null. That will never happen.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-19-2011, 06:17 PM #3
Oh shit, see I knew it was something simple. Its much harder to catch these things when the only time you can code is after a 12+ hour shift.
Thank you much sir, I'm going to test it out real quick.- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 04-19-2011, 06:21 PM #4
Similar Threads
-
problem with output from Array
By nellyman in forum New To JavaReplies: 5Last Post: 01-25-2011, 05:41 PM -
output not as expected.What's wrong?
By abdullahansari in forum New To JavaReplies: 1Last Post: 12-03-2010, 08:01 PM -
output not what expected!
By aza101 in forum Java AppletsReplies: 0Last Post: 07-10-2009, 06:17 AM -
Help with Array output Needed!
By 2potatocakes in forum New To JavaReplies: 2Last Post: 03-07-2009, 06:36 PM -
output from an array
By @eddie.com in forum New To JavaReplies: 5Last Post: 08-15-2008, 08:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks