Results 1 to 6 of 6
- 03-03-2011, 06:30 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 12
- Rep Power
- 0
Default value of String upon construction
Hello everyone, I'm new to this forum and to Java, but not programming. I have the below code. What I am trying to do is create a bag to store items in. I have an abstract class called Object, which right now is just a shell. My Item class is derived from Object. So what I want to do is create an Item called bag with say 10 slots, or whatever number, to serve as a player's inventory. I then want to add any item to the bag where there is an open slot. Right now I'm just trying to display the names of each item in a for loop which bombs when it reaches index 2 where i have not added an item.
I'm not quite sure I understand why. Do I need to create an empty Item of some sort and go ahead and fill in the remaining bag slots for this to work? For some reason I was under the impression that the array setup 10 objects and defaulted their members to something. Any help would be appreciated.
and...Java Code:public class Driver { public static void main( String[] args ) { Item bag[] = new Item[10]; Item potion = new Item( "Potion", "Consumable" ); Item sword = new Item( "Short Sword", "Sword" ); for ( int i = 0; i < bag.length; i++ ) { System.out.println( bag[i].getItemName() ); } } }
Java Code:public class Item extends Object { private String itemName; private String itemType; // Empty constructor Item() { } // Overloaded constructor Item( String n, String t ) { itemName = n; itemType = t; } public String getItemName() { if ( this.itemName.isEmpty() ) { return "Empty Slot"; } else { return this.itemName; } } }
- 03-03-2011, 06:35 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
An Object[] (any type of object) is initialised with a null reference in all slots until you put something else there.
- 03-03-2011, 06:39 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 12
- Rep Power
- 0
I see. So what would be the best way to output that a slot is empty?
EDIT: Figured it out, just check for null... originally I was checking for NULL which didn't work. Thanks.Last edited by Return 0; 03-03-2011 at 06:42 PM.
- 03-03-2011, 06:43 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Java Code:if (array[index] == null)
- 03-03-2011, 07:16 PM #5
It's bad practice to duplicate a class name that already exists in the standard JDK and doubly bad practice to duplicate a class name that exists in the java.lang package.I have an abstract class called Object ...
But it's triply (or worse) bad practice to duplicate the class name of the class that sits at the top of the Java class hierarchy: Object.
db
- 03-03-2011, 09:01 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Object Construction ends Program in GridWorld
By BJ1110 in forum Advanced JavaReplies: 9Last Post: 02-04-2011, 04:02 PM -
Need help with a construction of a tree of objects
By macwadu in forum AWT / SwingReplies: 5Last Post: 08-09-2010, 10:48 AM -
Copy Default table model to another default table model?
By greatmajestics in forum AWT / SwingReplies: 2Last Post: 04-28-2010, 04:08 PM -
[SOLVED] Access to default session deniedAccess to default session denied
By jazz2k8 in forum NetworkingReplies: 1Last Post: 03-10-2009, 01:12 PM -
AVL-tree construction
By student89 in forum Advanced JavaReplies: 0Last Post: 10-27-2008, 05:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks