Results 1 to 7 of 7
- 02-18-2012, 02:08 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 48
- Rep Power
- 0
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); */ } }This is not my entire program. It is the part I need help with.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 ); } }
- 02-18-2012, 03:32 AM #2
Re: Question about arrays. Please help.
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.I want it to print the array one line at a time
Where in your code are you trying to print the array? I don't see any print statements.
- 02-18-2012, 04:59 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 48
- Rep Power
- 0
Re: Question about arrays. Please help.
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!
- 02-18-2012, 11:57 AM #4
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
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..
- 02-18-2012, 01:38 PM #5
Re: Question about arrays. Please help.
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?I am trying to use JLabel to display the array
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
- 02-18-2012, 05:24 PM #6
Member
- Join Date
- Dec 2011
- Posts
- 48
- Rep Power
- 0
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:
How do I index? What does that mean?Java Code:InventoryDefine swordstone = new InventoryDefine("Sword In The Stone Movie", 1, 10, 13.00, 130.00);
- 02-18-2012, 05:27 PM #7
Re: Question about arrays. Please help.
You need to study how to use arrays. Read this:How do I index? What does that mean?
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
Similar Threads
-
A question on Arrays.asList()
By fatabass in forum New To JavaReplies: 11Last Post: 02-06-2012, 11:23 PM -
question about 2d arrays
By jaxber in forum New To JavaReplies: 2Last Post: 06-03-2011, 04:02 PM -
Question about 2D arrays
By fanle in forum New To JavaReplies: 7Last Post: 07-22-2009, 05:52 PM -
question about arrays
By broganm1 in forum New To JavaReplies: 3Last Post: 02-13-2008, 02:29 AM -
Question about arrays
By nhlfan in forum New To JavaReplies: 4Last Post: 11-15-2007, 11:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks