Results 1 to 7 of 7
  1. #1
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default Question about arrays. Please help.

    Hi there.

    I need help. I am trying to program an inventory program. I am trying to use Jlabel to print my array one line at a time. When I try to print the array it displays the memory address and I don't want that. I want it to print the array one line at a time. My code is below. How do I accomplish this? Can someone please show me how to do this? Thanks!

    Java Code:
    import javax.swing.JFrame;
    
    public class GUIRun {
    
        public static void main(String[] args)
        {
            InventoryAdvanced inventory = new InventoryAdvanced();
            inventory.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            inventory.setSize(300,300);
            inventory.setVisible(true);
    
           	//Define the calculation variables
    		double c1;
    		double c2;
    		double c3;
    
    		//Declare the calculation variables
    		c1 = 13.00;
    		c2 = 5.00;
    		c3 = 10.00;
    
    		//Declare the 5% restocking fee
    		double fee = .05;
    		double cost;
    		cost = 130.00;
    
    		//Create the objects
    		InventoryDefine swordstone = new InventoryDefine("Sword In The Stone Movie", 1, 10, 13.00, 130.00);
    		InventoryDefine hercules = new InventoryDefine("Hercules Movie", 2, 20, 5.00, 100.00);
    		InventoryDefine mermaid = new InventoryDefine("The Little Mermaid Movie", 3, 30, 10.00, 300.00);
            InventorySort sortThis = new InventorySort();
    		InventoryCal cal = new InventoryCal();
    		SwordStone movie = new SwordStone();
    
    		//Use an object to store the array
       		InventoryDefine[] inventoryArray = new InventoryDefine[3];
    
    		//Declare the arrays
       		inventoryArray[0] = swordstone;
       		inventoryArray[1] = hercules;
       		inventoryArray[2] = mermaid;
    
       		inventory.showArray(Integer.toString(inventoryArray));
    
       		/*//Call the method from the sortThis object
    		sortThis.Sort(inventoryArray);
    
    		//Call the method from InventoryCal
    		cal.calVar(c1, c2, c3);
    
    		//Override the method from InventoryInherit
    		movie.calVar(cost, fee); */
    
    
        }
    }
    Java Code:
    /*
    Author: Ben Hartnett
    Sources: Java: A Beginner's Guide
    Source 2: Monster example posted on student forums
    Date Retrieved: 1-26-2012
    */
    
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingConstants;
    
    public class InventoryAdvanced extends JFrame {
    
    		//Create the GUI
    		private JLabel label1;
    
    		//Add JLabels to JFrame
    
    		public InventoryAdvanced()
    		{
    			super( "JLabel" );
    			setLayout( new FlowLayout() );
    		}
    
    		public void showArray(InventoryDefine[] array)
    		{
    			label1 = new JLabel(array);
    			add( label1 );
    		}
    }
    This is not my entire program. It is the part I need help with.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,454
    Rep Power
    25

    Default Re: Question about arrays. Please help.

    I want it to print the array one line at a time
    Use a loop, get the elements from the array one at a time inside of the loop using the loop's control variable as the index and print them.

    Where in your code are you trying to print the array? I don't see any print statements.

  3. #3
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default Re: Question about arrays. Please help.

    Quote Originally Posted by Norm View Post
    Use a loop, get the elements from the array one at a time inside of the loop using the loop's control variable as the index and print them.

    Where in your code are you trying to print the array? I don't see any print statements.
    Can you show me an example of the loop? Reason I ask is because I am a visual learner. I am using JLabel to for the arrays. I am trying to use JLabel to display the array, but it keeps displaying the array memory address. Can you tell me how to fix that? Thanks! Much appreciated!

  4. #4
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Question about arrays. Please help.

    In your InventoryDefine class, define a toString method, and return whatever you will want to be printed.

    For example:

    IventoryDefine swordstone = new InventoryDefine("Sword In The Stone Movie", 1, 10, 13.00, 130.00);

    If "Sword In The Stone Movie" is the name of your InventoryDefine object and you want to print this when you say System.out.println(swordstone), you will something like this in your InventoryDefine class:

    public String toString()
    {
    return name;
    }

    Now I see that you have an array called: *inventoryArray which holds InventoryDefine objects.

    In your main method try this please:

    for(InventoryDefine inventory: inventoryArray)
    {
    System.out.println(inventory);
    }

    The JLabel part, you can figure out yourself I guess..

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,454
    Rep Power
    25

    Default Re: Question about arrays. Please help.

    I am trying to use JLabel to display the array
    What are you expecting to see in the JLabel? Will the size of the array always be small enough to contain all of the array's contents?
    You could show the element at location ix by indexing the array:
    theArray[ix]
    will reference the element at location ix.
    Change the value of ix to see different elements in the array

  6. #6
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default Re: Question about arrays. Please help.

    I am expecting to see the inventory name, product, etc...I am expecting to see this information in the parameters:

    Java Code:
           InventoryDefine swordstone = new InventoryDefine("Sword In The Stone Movie", 1, 10, 13.00, 130.00);
    How do I index? What does that mean?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,454
    Rep Power
    25

    Default Re: Question about arrays. Please help.

    How do I index? What does that mean?
    You need to study how to use arrays. Read this:
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

Similar Threads

  1. A question on Arrays.asList()
    By fatabass in forum New To Java
    Replies: 11
    Last Post: 02-06-2012, 11:23 PM
  2. question about 2d arrays
    By jaxber in forum New To Java
    Replies: 2
    Last Post: 06-03-2011, 04:02 PM
  3. Question about 2D arrays
    By fanle in forum New To Java
    Replies: 7
    Last Post: 07-22-2009, 05:52 PM
  4. question about arrays
    By broganm1 in forum New To Java
    Replies: 3
    Last Post: 02-13-2008, 02:29 AM
  5. Question about arrays
    By nhlfan in forum New To Java
    Replies: 4
    Last Post: 11-15-2007, 11:38 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
  •