Welcome to Dream.In.Code
Become a Java Expert!

Join 150,164 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,355 people online right now. Registration is fast and FREE... Join Now!




can someone help me create an array of non primitives

 
Reply to this topicStart new topic

can someone help me create an array of non primitives

nhackett
24 Aug, 2008 - 07:12 AM
Post #1

New D.I.C Head
*

Joined: 24 Aug, 2008
Posts: 3

in the class 'character' i want to set up an array of objects from the class 'item'

CODE
static item[] inventory = new item[51];


In the class 'item' variables are set up to keep track of information such as item names

CODE
static String itemName = "Big Cool Sword";


In my main program I want to change the value of one of these items

CODE
public static void main(String[] args) {
character hero = new character();
hero.inventory[1].itemName = "Bigger Cooler Sword";
}


But changing the value of
CODE
hero.inventory[1].itemName
to anything seems to change the value each
CODE
itemName
in the array, not just the
CODE
itemName
at position [1].

I know this because I output all the info with a call to
CODE
printCharacterInventory();
which is a function in the character class.
CODE


        public void printCharacterInventory(){ // use this to print out inventory items
        int count = 0;
        while (count <= 50) {
            System.out.println( "inventory " + count + ": " + " ********************");
            System.out.println( "itemName: " + inventory[count].itemName );
    }


My output lists "Bigger Cooler Sword" for each itemName, but I want only the itemName in hero.inventory[1] to be "Bigger Cooler Sword."
What in the world am I doing wrong?

If you are wondering why I only keep track of itemName, I removed a LOT of other variables to keep this post as short as possible.

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Can Someone Help Me Create An Array Of Non Primitives
24 Aug, 2008 - 07:26 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
In your while loop you do not increment your count variable. So each time through the loop count = 0 which is your first element. Put in count++; inside your while loop and you should see some changes.

Edit: Also make sure you are matching up your curly braces correctly, your example is either missing the last half of the function or you have a curly brace mismatch. Just a tip.

Hope that helps you out. smile.gif

This post has been edited by Martyr2: 24 Aug, 2008 - 07:27 AM
User is offlineProfile CardPM
+Quote Post

nhackett
RE: Can Someone Help Me Create An Array Of Non Primitives
24 Aug, 2008 - 07:47 AM
Post #3

New D.I.C Head
*

Joined: 24 Aug, 2008
Posts: 3

QUOTE(Martyr2 @ 24 Aug, 2008 - 08:26 AM) *

In your while loop you do not increment your count variable. So each time through the loop count = 0 which is your first element. Put in count++; inside your while loop and you should see some changes.

Edit: Also make sure you are matching up your curly braces correctly, your example is either missing the last half of the function or you have a curly brace mismatch. Just a tip.

Hope that helps you out. smile.gif



Thanks for the help, but those are mistakes I made while getting code into my post. I'll edit the original to make more sense later, but its time for church right now. My original code has the counter increment and necessary bracket.
User is offlineProfile CardPM
+Quote Post

thenovices
RE: Can Someone Help Me Create An Array Of Non Primitives
24 Aug, 2008 - 08:32 AM
Post #4

D.I.C Head
**

Joined: 18 Jan, 2008
Posts: 73



Thanked: 7 times
My Contributions
you made the itemName static, which basically means theres only one instance of the string itemName which ALL item objects share. So all items would always have the same itemName. The solution is to remove the static modifier thingy and you'll be good.
User is offlineProfile CardPM
+Quote Post

nhackett
RE: Can Someone Help Me Create An Array Of Non Primitives
24 Aug, 2008 - 03:14 PM
Post #5

New D.I.C Head
*

Joined: 24 Aug, 2008
Posts: 3

QUOTE(thenovices @ 24 Aug, 2008 - 09:32 AM) *

you made the itemName static, which basically means theres only one instance of the string itemName which ALL item objects share. So all items would always have the same itemName. The solution is to remove the static modifier thingy and you'll be good.


That is exactly the answer I was looking for. Everything works fine now. Thank you; you are a gentleman(woman) and a scholar.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:51AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month