Results 1 to 4 of 4
  1. #1
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default 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
    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
    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
    Java 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?
    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.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    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!

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Ok now it works, but I forgot to add break to my class file so it assigned dagger to all the slots lol.

    Alright mate, thanks again. +rep
    • 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.

Similar Threads

  1. problem with output from Array
    By nellyman in forum New To Java
    Replies: 5
    Last Post: 01-25-2011, 05:41 PM
  2. output not as expected.What's wrong?
    By abdullahansari in forum New To Java
    Replies: 1
    Last Post: 12-03-2010, 08:01 PM
  3. output not what expected!
    By aza101 in forum Java Applets
    Replies: 0
    Last Post: 07-10-2009, 06:17 AM
  4. Help with Array output Needed!
    By 2potatocakes in forum New To Java
    Replies: 2
    Last Post: 03-07-2009, 06:36 PM
  5. output from an array
    By @eddie.com in forum New To Java
    Replies: 5
    Last Post: 08-15-2008, 08:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •