Reference is printed instead of the details of the object

I have a class like this:
public class Product {
    private String productName;
    private String description ;
    private double unitCost ;
    private String productId;
    private int quantity;

    public Product(String productName, String description, double unitCost, String productId,
            int quantity){
        this.productName = productName;
        this.description = description ;
     this.unitCost = unitCost ;
        this.productId = productId;
        this.quantity = quantity;
    }

    // All get and set Methods for variables go here....

    public String displayProductInfo(){
         return productName + " " + description + " " +  productId + " "
                 + unitCost + " " + quantity;
    }
}
I have a second class
import java.util.ArrayList;
import java.util.Iterator;

public class Stock {

    ArrayList<Product> listOfItems = new ArrayList<Product>();

    public void addItemToStock(Product item){
        listOfItems.add(item);
    }

    public void removeItemFromStock(Product item){
        listOfItems.remove(item);
    }

    public void display(){
        Iterator itr = listOfItems.iterator();
                while(itr.hasNext()){
                    System.out.println(itr.next());
                }
    }
    
}
and my test class is
public class TestClass {
    public static void main (String [] args){
        Product product1 = new Product("Fridge", "New", 125.99, "FR435", 5);
        Stock stock = new Stock();
        stock.addItemToStock(product1);
        stock.display();
    }
}
My output is Product@addbf1
I think I know what is the output and I know a way to solve the problem, but I would like to find a way to use the ArrayList instance to print the products instead to add a method to my product class to return all the information about the product as string. Any help would be greatly appreciated.

To use System.out.println () with an object, you must override the toString() method of java.lang.Object .

public String toString()
{
  return new String("Whatever information you wish to print");
}

Edit: This is the method that SOP calls when used on a descendant of the object.

Edit2: / sigh to himself. Nope, he calls String.valueOf (Object) (calling ToString unless the object reference is null) and prints the resulting string. Then call println(). I'm sorry.

http://download.Oracle.com/javase/6/docs/API/Java/IO/PrintStream.html#println%28Java.lang.object%29

http://download.Oracle.com/javase/6/docs/API/Java/lang/string.html#valueOf%28Java.lang.object%29

Published by: 808061 on 07.11.2010 17:08

Published by: 808061 on 07.11.2010 08:18

Tags: Java

Similar Questions

  • How to make the sign of the pound Sterling to print instead of the hash symbol?

    When I type and need to use the symbol for the pound Sterling (Upper case 3) I get only the symbol # (sharp).  How can I change this.  I tried to change the keyboard to English instead of e. u., but this has no effect.

    option / alt + 3

  • My layer mask is using the entire layer as a mask instead of the object

    I have the letters that I've converted to outlines and are grouped.  I need a picture to be obscured by these letters.  I created a layer with the letters layer mask, but on the collapse of the group, all the individual letters are masks, but the Group layer mask image icon is removed all that actually hide the entire layer.

    If I separate the letters and then make a layer mask, a letter is a mask, the rest are ignored.

    What I'm doing wrong here?

    Nicola,

    To have the letter several forms works as a mask, select all and opposition > compound path > make. That's all you need (no need of a mask layer or anything else).

  • On some pages, instead of the address of the page on the address bar, I get a mixture of icons (rss, home, print, refresh, search etc.). Downloaded again, but did not.

    On some pages (like gmail.com), instead of the address of the page on the address bar, I get a mixture of icons (rss, homepage, Star, previous-next, print, refresh, search etc.). I can not copy the address or refresh the page. I downloaded firefox again, but it did not work.

    Hello Kadomar, other users have reported that this error can be caused by third-party extensions like Icon RSS 1.0.6, which apparently is no longer maintained.

    Troubleshoot extensions, themes, and issues of hardware acceleration to resolve common problems of Firefox

  • HP printer Assistant opens two notebooks on HP Officejet 6500 a more instead of the wizard

    I have a HP Officejet 6500 has more used with Windows Vista 32-bit, and wireless. When I click on the printer icon to open the Printer Wizard, it opens two documents Notepad instead.  The first is the HPSPProgress.hta - block-notes, and the second is the HPSolutionsPortal.hta - block-notes.  I used the control panel to uninstall the printer and then went to HP.com to reinstall the printer twice.  This has not changed the problem.  I also have a laptop computer that prints on the same printer wireless and everything works fine.  When I click the icon on it, it gets the necessary information, and then opens the Printer Wizard.  Is there something that I am missing here or what I would do.  I also ran the tools of diagnosis HP printer and it returns the printer works properly. There has been no other changes or programs installed before the occurrence of the issue.

    Hi stwy2hvn,

    I'd take a peek at this post by pcwizard.  This solution has worked for others.  Let me know how it goes.

  • Installs the ESC/P-R instead of the Epson Stylus CX4800 printer when I plug in the printer.

    Original title: ESC/P-R installs, instead of CX4800.

    My work computer has been upgraded to Windows 7.  When I plugged my printer Epson Stylus CX4800, he installed ESC/P-R.  I went on the Epson site and downloaded the correct driver, but he continues to reinstall the ESC/P-R.  I tried to add manually, but since it connects via USB, it does not appear in the list as a choice.  Any suggestions?

    Thank you for your answer!  I had to work, but not exactly with the steps you described.  I did not know exactly if the uninstall of the printer actually removed the drivers and didn't know how to manually remove printer from my computer drivers.  What I did was downloaded from the Epson site 'drivers and utilities package' for Windows 7, and not only the printer drivers.  Once downloaded, I ran it and it is installed the correct printer.

  • EPSON STYLUS CX4800 print the image comes out blue instead of the other colors

    Original title: printer

    Each time, I printed the picture comes out blue instead of the other colors, I have only blue. If I want to print a photo of family is blue spell. What can I do I have an Epson STYLUS CX4800

    Hello

    1. What is the color of ink cartridge in the printer?

    2. the problem occurs only in print images?

    You can search the print settings in the printer tool and select the color according to the requirement. Also make sure that the cartridges are properly ventilated and locked.

    Epson Stylus CX4800 all-in-One Printer

    http://www.Epson.com/cgi-bin/store/support/supDetail.jsp?BV_UseBVCookie=Yes&infoType=FAQ&OID=58606&prodoid=56291067&FOID=63653&cat=30961&SubCat=30963

    You can watch the frequent question on the Epson Stylus CX4800 printer:

    Epson Stylus CX4800 all-in-One Printer - FAQ

    http://www.Epson.com/cgi-bin/store/support/supDetail.jsp?BV_UseBVCookie=Yes&infoType=FAQs&OID=58606&prodoid=56291067

    Alternatively, you can contact Epson for more assistance on the same:

     

    Welcome to Epson support

    http://www.Epson.com/cgi-bin/store/support/SupportIndex.jsp?BV_UseBVCookie=Yes&expansion=1000000

    Hope this information helps. If you need help with Windows, just tell me and we will be happy to help you.

  • How can I PERMANENTLY change the default printer to Adobe PDF files using my physical printer instead of SIMPLY printing a pdf in a new PDF file?

    Whenever I restart Adobe Reader DC default printer is automatically reset to print any PDF file to the "print to PDF file" as the default printer.  How can I change the "default printer" always default to be my physical default printer, instead of "print of PDF?»»»

    PRINTING a document which is already a pdf file to a pdf file is absolutely ridiculous and stupid for the following reasons:

    1. If you want to save a document that is already a pdf in different PDF file, you can use the function "Save as" in the file menu.
    2. If the user select "Print" to PRINT a file they ther NOT try to SAVE a file.  (We'll give credit to the average user to understand the difference between PRINTING and SAVING of a document).
    3. Most of the users do not need to duplicate the storage of a document on their drives to computer
    4. Users of mobile devices, by the fact that they have already downloaded the PDF file have already used storage space of EXTREMELY valuable and LIMITED memory with the download of origin and most certainly so NOT to use more than this space such a resource limited printing!

    As an engineer, hardware and software for the past 40 + years to really understand the value of making easy allow the end user to change the configuration of something as simple as the setting for the default printing device to use!

    For the record, I searched the online help as well as the forum Adobe Reader community for a thread that explains how to change the printer by default so that whenever I starts the Adobe service it will automatically use the printer default physical connected to my computer to "PRINT" any PDF document.  If I missed something that deals with this problem please point me about it, if no such band rolling exists, then please tell me before making this change.

    Thank you for your help in this matter!

    Sincerely,

    Larry A. Coates

    Hi larryc57256426,

    You can set your default printer following the steps mentioned in this document KB change the default system printer.

    Check in the print dialog box, you have unchecked "print to file" under "Advanced settings."

    Kind regards
    Nicos

  • print the data of the form instead of the whole page

    Hi all

    I'm having a problem in my ADF/JSF page. I just want to print the data in the form of my form instead of the whole page.using javascript:print() method prints the whole page. so can you help me solve this problem. I JDev 10.1.3 and do not want to upgrade to 11g JDev!

    Is there a java code to do?

    Thank you

    Hello

    You can apply CSS to the page and use the media to deermine switch which components will appear in print and who should not.

    Download the word document in http://thepeninsulasedge.com/frank_nimphius/wp-content/uploads/2008/05/OldAndLostBlogBusterTips.zip

    and take a look at page 287

    Frank

  • When you use print selection in Firefox 12.0, it prints whenever the selection is on the page (on paper) instead to the top of the page (in paper).

    When you try to use the print selection, printing starts in the location it starts as on the web page. For example, if it is down half way down the webpage, it starts halfway to print on printer paper. Then continue to another document. This only happens in Firefox, not other browsers. It makes me really want to change the default browser so I don't have to keep switching to print correctly. I tried Print Edit plugin, but it is a lot of time to do something that should be simple.

    Yes, the functionality of the print selection was twisted several times in recent versions. It was very broken in Firefox 11 and the fix has exacerbated the problem of the position of the selection moves to the top of the page (which has a long history, but is worse now).

    Workarounds

    I agree that the add-on Print Edit, which allows very fine control of the elements on the page, is not the best solution for this problem, because you often click on number of items for the same selection.

    The add-on Nuke anything that improved , has the ability to delete everything except your selected page text. Then you can print normally (i.e., without choosing the bubble of selection). I see not a feature cancel it, so you need to reload the page later to continue working with her.

    Some screenshots illustrating this use will appear below this post.

    (Note: the wire earlier How to get selection impression at work, page 2.)

  • I have a problem with the American version of Firefox layout for printing in millimeters and the default page as A4 instead of the letter size.

    The default page size is A4 instead of the letter in the American version of Firefox (10.0.2). I know it was the letter before. The margins were 0.5 inch now is 12.7 millimeters. What happened twice. I unloaded the extensions I thought that maybe caused this, but nothing helped. It is a pain to export all bookmarks, uninstall Firefox and remove all traces of the program and reinstall. Is there a way to default on letter with thumb size margins?

    I deleted the resolved status as you indicate that you have not fixed yet.

    You opened the topic: config page through the address bar and put impression in the filter at the top?

    See also:

  • using three colors instead of the HP C4780 black print cartridge

    can I set up my printer to use the cartridge three colors to black and white or regular print?  (I buy black cartridges and lights all but seem to only use black).  THX!

    Hi winter1420,

    Welcome to the HP forums!

    I see that you want to print using only the color on your Photosmart C4780 cartridge. I'd be happy to help you with that

    Looking the Page 50 of your User Guide, I would say that Yes, it is possible to configure the printer to print with just the cartridge three colors. Please note that your black from the color cartridge is perhaps not exactly black as explained on page 50 in the Guide of the user as well.

    Hope that helps! Please let me know if you have any other questions!

  • Paper feeds through the back instead of the round print

    I have a HP Photosmart C4780, Windows 7, error message Out of Paper, no changes to the operating system

    The printer will feed the paper, but shoot it the back of the printer rather than enter this round and print, I was trying to print on cardstock, when the issue occurred, but continues to to happen when I try to regular printer paper as well. I followed the steps for a printer feed error I found at this link: http://support.hp.com/us-en/document/c02065582#N1040 but it did not help.

    Thank you very much for your help!

    I suggest you remove the rear panel as shown in the video on this page to troubleshooting, then replace the Panel back and make sure it snaps in place.

  • Printer does not print without saving the file first

    Whenever I try to print an article I have to register all began as before that I could request a printing and in the left pane, at the top, it looks like 'print', but now it is said that 'save' what I have to do to correct and be able to print without saving the first element. I have a Brother MFC-J5910DW all-in-one. Excellent machine.

    Hello Geoff,

    Thanks for posting your question on the Microsoft community.

    I would like to know some information about the problem so that we can help you better.

    You made any hardware or software changes on your computer before the show?

    Thank you for details on the issue.

    This problem can occur because the parameters or corrupt device drivers.

    I would suggest trying the following methods and check if it helps.

    Method 1:
    Run the Print Troubleshooter and check.
    Reference:
    Open the printer Troubleshooter
    http://Windows.Microsoft.com/en-us/Windows7/open-the-printer-Troubleshooter

    If this does not help, use method 2.

    Method 2:
    Please use the suggestions contained in this Microsoft support article and check the issue.
    Reference:
    Solve printer problems
    http://Windows.Microsoft.com/en-us/Windows/printer-problems-in-Windows-help#fix-printer-problems=Windows-7&V1H=win8tab1&V2H=win7tab1&V3H=winvistatab1&v4h=winxptab1

    Please also update the printer drivers from this link and check.

    http://support.brother.com/g/b/downloadend.aspx?c=us&lang=en&prod=mfcj5910dw_all&OS=93&dlid=dlf004746_000&flang=8&Type3=11

    I hope this information helps.

    Please let us know if you need more help.

    Thank you

  • Black photo using instead of the black regular for the general impression

    I have a printer C309a All In One Photosmart Premium.   He left black printing, so I replaced the black ink cartridge large and it would not print in black.  I checked ink levels in the settings and it says black of the Photo came out!

    I do not print photos.  Why he uses photography in black instead of the ordinary black?

    I have verified that my specific print settings 'General everyday printing"rather than photos, etc. I looked through all the settings on the hardware and the software, but was unable to find anything that solve this problem.

    Anyone know what I can do to make the printer to use the wide use, black General rather than the photo black ink?

    Covcomm wrote:

    OK, thanks for the quick response.  I tried to put into service of "plain paper" and reproduced, but still the black does not appear.  The other colors are, then it must use more black photo than the general use of the black.

    If I understand your answer, the printer requires that both black cartridges have ink to print any black, even if I'm not print photos?

    OK, if the printer goes through the motions of printing but does not print black it would indicate a problem with the print head.  Try to run up to three cycles of cleaning printhead from the Toolbox.

    While a photo of small amount black (and all other colors), ink will be used to serrvicing it will not print on paper for normal text.

Maybe you are looking for