aspose file tools*
The moose likes Beginning Java and the fly likes Unable to display data from an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unable to display data from an ArrayList" Watch "Unable to display data from an ArrayList" New topic
Author

Unable to display data from an ArrayList

Dan D'Apice
Greenhorn

Joined: Sep 26, 2012
Posts: 24
Hello all,
This is my first post, so sorry if i post something wrong here. Anyway, I am working on a class assignment and I need to meet the following requirements:

• Modify the Inventory Program so the application can handle multiple items. Use an ArrayList to store the items. DO NOT PROMPT USER FOR INPUT; initialize the array with hard coded values.
• The output should display the information the first product in your ArrayList, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product.
• Create a method to calculate the value of the entire inventory. Call this method and display the results. (Hint: Use a loop to make your calculation.)
• Create another method to sort the array items by the name of the product.

I was able to code for part 1 and part 3 of the above requirements, but part 2 (displaying) and part 4 (sorting) I am completely stumped!! I have tried multiple variations of overriding a toString() method but just get errors. What in God's name am I missing? Code follows:





Any help would be greatly appreciated...Thanks in advance...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Take one piece at a time.

What exactly did you try for the display part, and what exactly was the problem with it?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

And welcome to the Ranch!
Dan D'Apice
Greenhorn

Joined: Sep 26, 2012
Posts: 24
Hi Jeff,
Thanks for the reply. Well, I am looking to display at least one instance as follows:

Item # Name In Stock Price each
001a Java 101 50 19.99
001b Python 101 50 19.95 - this output can be optional
001c C ++ 101 25 25.99 - this output can be optional

What I get for output is:

inventory.Book@1c8825a5
inventory.Book@2e5f8245
inventory.Book@6197cc

The line with the Item #, etc. displays fine. It has been suggested to me to do an override of a toString() method, but I wouldn't know where to insert that...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Your Book class does not override the toString() method, so it uses the one it inherits from Object, which just prints the class name and a number that may correspond roughly to the hashCode or address or somesuch.

Java doesn't know to turn objects into Strings. It has to be explicitly told.

Dan D'Apice
Greenhorn

Joined: Sep 26, 2012
Posts: 24
OK, so that's where the override comes in? I'm going to give it a try and see what other errors I get...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Dan D'Apice wrote:OK, so that's where the override comes in?


In the general case, overriding is used when you want a subclass to provide different behavior than its parent class. In this particular case, you're calling System.out.println(X), which ultimately ends up calling X.toString(). Since you didn't override toString(), you got the parent's version--Object's version.


I'm going to give it a try and see what other errors I get...


That's the spirit!
Corrie L Sherone
Greenhorn

Joined: Jan 21, 2008
Posts: 19
Not to discourage you from learning about overriding but there is more than one way to skin a cat ("ouch"!). Do not forget your Book class has all those perfectly good methods for obtaining the name, price etc. All you need is a Book object from your list.
Dan D'Apice
Greenhorn

Joined: Sep 26, 2012
Posts: 24
So, after some time I've gotten a little further. I love Java...NOT!!!...what a PITA!! OK, vent session is over. Here is what I have so far:




My output is as such:


I still need to populate those other columns with the respected data from the arraylist. I feel like I've been pushed into the deep end of the pool without being taught how to swim....now what am I missing? Thanks for all the help so far, everyone :-D
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Aren't you misusing String.format ?


[My Blog]
All roads lead to JavaRanch
Corrie L Sherone
Greenhorn

Joined: Jan 21, 2008
Posts: 19


Just my opinion but String.format may be a little advanced at this stage of the game. While I've used it .. I still have to look up the details of the format specifiers every time But to answer your question, if you look at the documentation the method expects a "format string" followed by a list of arguments ie



In your code you've supplied the arguments: itemNumber, itemName, etc... - but forgot the format string.

As you already have Book objects, why not just use existing the methods in the Book class to get the name, price, etc..?


Saket Kumar Sinha
Ranch Hand

Joined: Aug 04, 2011
Posts: 32


What is the problem in overriding this way
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

What is the problem in overriding this way

With all members values concatenated like this, it's going to be difficult to read.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Corrie L Sherone wrote:Not to discourage you from learning about overriding but there is more than one way to skin a cat ("ouch"!). Do not forget your Book class has all those perfectly good methods for obtaining the name, price etc. All you need is a Book object from your list.
That means other classes need to “know about” the book class. Not at all object‑oriented programming; the book class should take care of itself. If you change the implementation of the book class, (or even worse, its public interface), such a method will be broken. What if you add an author field to the book class later?
Overriding (and polymorphism) is the way to go.
Dan D'Apice
Greenhorn

Joined: Sep 26, 2012
Posts: 24
Hello All (again),
Thanks to everyone again for the great suggestions. I used Saket's suggestion. Here is what I have as of this morning:


OUTPUT:


Not sure why the output is distorted within the code tags? I realized I "cheated" in spots by tweaking the output with additional "\t", but it looks even. I am not sure why the data from index (1) came out misaligned, but the tweaks worked. Now on to the last part:

• Create another method to sort the array items by the name of the product

I'll start off by reading our God awful text from class and see what I can come up with. Wish me luck, I'm going to need it...
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Dan D'Apice wrote:So, after some time I've gotten a little further. I love Java...NOT!!!...what a PITA!!

I doubt whether it's Java specifically. All programming languages have their foibles; and ALL of them require you to be precise.

I feel like I've been pushed into the deep end of the pool without being taught how to swim....now what am I missing?

I think everybody else is giving you good advice on the mechanics, so I'm going to concentrate on the data.

What is a Book? Is it a Title? Is it hardback? Paperback? Braille? Audio? What is it's "price": MSRP?

Don't get too bogged down with it, but the fact is that a Book is a Book, not an item of your Stock; and you may well have several items of Stock, which all equate to the same Book.
Now you've abstracted that by having a stock quantity in your Book class, which is understandable; but don't Lamps and Playing Cards and TV's also have stock quantities? If you set it up that way, you'll have to repeat the stockQuantity for every type of Stock item you hold, and you'll also have to repeat the logic to increment and decrement it properly.

What about, instead, having a StockItem class that holds all the information you need about any item? - Eg: Quantity in Stock, Quantity on Order, Unit price, etc. - and then have that StockItem reference a particular Book, or TV, or whatever?

Do you see how that models your inventory better? "Inventory" then becomes a List of the StockItems you hold; and a StockItem can be anything from Gone With The Wind, to a Sony PlayStation, to a tin of Beans. Furthermore, it separates "what it is" from how it gets dealt with by your Application.

A simple, but easy, mnemonic for thinking about Stock is "CIC", or "Category-Item-Copy":
Category: Something that categorizes an Item - eg, a Book; but it could be something broader, like "Dried Goods".
Item: A specific Book: eg, Gone With The Wind.
Copy: An individual copy of that Book on your shelves. In larger stores these will often be barcoded, but I wouldn't worry about that in your case; your 'quanitity' fields should be just fine as a generalization of a 'copy'.

In large systems, there may be more than one level to each of those definitions, but you'd be surprised how often that basic pattern holds up.
Don't sweat it too much, but maybe it's given you a few things to think about.

HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Corrie L Sherone
Greenhorn

Joined: Jan 21, 2008
Posts: 19
Campbell Ritchie wrote:That means other classes need to “know about” the book class. Not at all object‑oriented programming; the book class should take care of itself. If you change the implementation of the book class, (or even worse, its public interface), such a method will be broken. What if you add an author field to the book class later?
Overriding (and polymorphism) is the way to go.


Hm.. I was reading the requirements a little differently. I didn't think they were specifically asking for a method that dumped *all* properties of an object. If that's the case, then I agree. The implementation should be up to the class itself. My take was the simple program needed to print out specific properties - like for a report. In which case it normally would need to know about specific methods of that object. Granted I haven't had my coffee yet, so maybe I'm still off base here.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I think we are in agreement, if you take the different requirements into consideration
Dan D'Apice
Greenhorn

Joined: Sep 26, 2012
Posts: 24
Hello All,
Just wanted to give a quick update: I wasn't able to figure out (from the syllabus or our wonderful text) the sorting requirement. I input the following code in for good measure and it didn't return any errors:


There is another requirement that is due Sunday, but right now I need to de-tox from coding for a bit. I am not cut out for this. 3 more weeks left to class and I am humbly bowing out. Thanks again to everyone for your awesome suggestions and support! You all have what it takes...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You should only have a compareTo() method if there is a “natural order” to books. Look at this interface. Do you sort books by author, by title, by ISBN, or what?
If you have been told to use that method, then I suggest you do three things:-
  • 1: Add “implements Comparable<Book>” after the class name and before the first {
  • 2: Change the method to take Book b as its parameter rather than Object o
  • 3: Get rid of the (Book) cast.
  • If books don’t have an order, you can create Comparator<Book>s for sorting by different criteria. You can find more about sorting here.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    If you go through those links, you may find that the ready‑made sorting methods require a Comparable or a Comparator.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    … and, to get me a bit closer to 30000 posts, have a look at the java.lang.String (←link) class and look for Comparable and Comparator and a compareTo method. See if that helps you work out what your compareTo method is doing.
    Corrie L Sherone
    Greenhorn

    Joined: Jan 21, 2008
    Posts: 19
    Dan D'Apice wrote:There is another requirement that is due Sunday, but right now I need to de-tox from coding for a bit. I am not cut out for this. 3 more weeks left to class and I am humbly bowing out. Thanks again to everyone for your awesome suggestions and support! You all have what it takes...


    Well not everyone chooses to program for a living. Even if you enjoy it, it still takes study and practice like any field. But do give yourself some credit. You completed two parts on your own and your instincts about the others were good ie Overriding toString and that you need to use a Comparable or a Comparator for sorting.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Unable to display data from an ArrayList