Results 1 to 14 of 14
  1. #1
    Beaner462 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Post Building an inventory program that uses an array but has string values as well as int

    Hello all. I am trying to build a program that stores inventory for selected products and must use an array. My problem is that the inventory is supposed to show the product name, item number, department, units in stock and price. I think I have figured out how to create an array using just integers but I need string values such as the name and department it falls under. My instructor says this is possible using just one array, however, I don't think it matters if I do use two arrays, or even five if needed for the products in inventory. I would rather only use one array but this week I need to modify the program so it shows off more than one product (and its details listed above). Anyone have any ideas that I could use just one array with the integer values but still display the name and department of the products?

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Is this an assignment? You might want to take a look at 2D array.
    Here is a tutorial in java about JTable which uses 2D array to fill data.
    How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

  3. #3
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    can you make a product class that has all those fields and then make an array of products?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Quote Originally Posted by yellowledbet View Post
    can you make a product class that has all those fields and then make an array of products?
    This, if you have covered classes.
    If not, then mine0926's suggestion...a 2D array of Objects, which will allow to to use String, Integer, etc etc objects.

  5. #5
    Beaner462 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    I could use objects in the array and then have all these details i need in the objects. I also need to have the program do a total value of the inventory (units in stock of each camera * price), is this possible using objects from the array?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    is this possible using objects from the array?
    Yes, that would work.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Is Product and Inventory season early this year? It's usually around September.

    db

  8. #8
    Beaner462 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Ok, so I am really REALLY lost here. I am trying to start over again. Let's say for my inventory program to call up an array of objects class that I create, the product, can I use an int array to call up these objects? Like this

    int array[] = { product1, product2 }

    and in my other product class I need to declare these

    product1 = (productName, productItemNumber, etc.)
    product2 = (pruductName, productItemNumber, etc.)

    Can I assign these to the integers 1 and 2 in the int array? I need this program to display my product's details that have double and string values.
    I am so lost and my instructor only emails me very late and I was supposed to turn this in today
    Can anyone help me get started here? I am looking at this wrong or approaching this the wrong way?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    You should look in the Java Tutorial about arrays:
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    The syntax for creating an array is:
    <THE TYPE>[] aName = new <THE TYPE>[<nbr of elements>];
    Where <THE TYPE> is the type of the data held in the array. int is a type, String is a type. Or it could be a class that you define.

    To get one of the elements of an array use the syntax: aName[<index for element]. That will return the element which will be of <THE TYPE> data type. For example:int, String, your class....

    Before worrying about putting your class in an array, get a design for your class. What data will it hold and how will you put data into it and get data from it.

  10. #10
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    @OP - It is hard to suggest when you do not see the logic or what you are trying to do.

    Here is an example of 2D Array which holds a group of item. Each item have their related details such as manufacturer, price and current stock
    Java Code:
    Object[][] anArray2d = new Object[][]{
        {"JM019", "DELL", new Double(1049.55), new Integer(165)},   //--|
        {"SH2448", "AOC", new Double(899.99), new Integer(98)},     //  |
        {"EQ755", "ACER", new Double(127.34), new Integer(134)},    //  |>>> ROW
        {"EQ764", "ACER", new Double(127.34), new Integer(256)},    //  |
        {"KG875", "COBY", new Double(127.34), new Integer(134)}     //--|
    };
    Can you post some code that is related on your problem?

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Quote Originally Posted by mine0926 View Post
    Here is an example of 2D Array which holds a group of item. Each item have their related details such as manufacturer, price and current stock
    Yuck

    db

  12. #12
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Why yuck?

    mine0926

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Because the OP has said:
    "I could use objects in the array and then have all these details i need in the objects."
    Which means they can create a class to hold the data, so using an array to hold that class data is a complete hack.

    Or, put another way...yuck.

  14. #14
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    @Tolls and DarrylBurke - Thanks for clarification and for correcting me.

Similar Threads

  1. Problem with String Building
    By Albert in forum New To Java
    Replies: 2
    Last Post: 04-30-2012, 12:49 AM
  2. Sorting string Array Values
    By Zerpol in forum New To Java
    Replies: 1
    Last Post: 01-14-2011, 07:08 AM
  3. My Inventory Program
    By ladykrimson in forum New To Java
    Replies: 28
    Last Post: 11-01-2010, 09:06 PM
  4. Help with Inventory Program, Please
    By NewKidjava in forum New To Java
    Replies: 4
    Last Post: 08-21-2009, 11:09 AM
  5. Inventory program
    By Nexcompac in forum New To Java
    Replies: 3
    Last Post: 07-27-2007, 05:51 PM

Posting Permissions

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