Hi, I'm new to Java and programming. I have an assignment I'm trying to do and I'm pretty stumped on this question.
I have a TreeMap where the keys are Strings (of musical artists) and the values are Lists (of artist name, album title, album genre and album year).
I have a method to do that display all information for every CD which I have iterated through with a foreach loop. The problem is that I can't get the List values to display all the information when I print, I just get the hash values? of each list bu I need it to print the information in the list. eg. The Sex Pistols, Nevermind the Bollox, Punk, 1977.
Can anyone tell me how to do that?
Cheers x

This isn't very clear - but do you mean that those details are fields of a class you defined (eg Album) and when you print it you get some hex?
If so, that's the default toString() method that your class has inherited from Object, and what you need to do is to define your own public String toString() method in your class - return a String that describes the contents. That's what will be printed when you print an instance of this class.
If not, please be more specific!

Sorry I wasn't clear on this.

I have two classes, a CD class that looks like this;

public class CD
{
   /* instance variables */
   private String artist;  // name of artist or composer
   private String title;   // title of album
   private String genre;   // genre of album
   private int year;       // year of album release or composition
  

   /**
    * Constructor for objects of class CD
    */
   public CD(String anArtist, String aTitle, String aGenre, int aYear)
   {
      super();
      this.artist = anArtist;
      this.title = aTitle;
      this.genre = aGenre;
      this.year = aYear;
   }
   
   /* instance methods */

   /**
    * Returns the receiver's artist
    */
   public String getArtist()
   {
      return this.artist;
      
   }
  
   
   /**
    * Returns the receiver's title
    */   
   public String getTitle()
   {
      return this.title;
   }
   
   
   /**
    * Returns the receiver's genre
    */   
   public String getGenre()
   {
      return this.genre;
   }
   
   
   /**
    * Returns the receiver's year
    */
   public int getYear()
   {
      return this.year;
   }
   
   
   public void printString()
   {
      System.out.println (this.artist + ", " + this.title + ", "
                          + this.genre + ", " + this.year +".");
   }

And a MusicLibray class that so far looks like this;

public class MusicLibrary
{
   /* instance variables */
   
   private Map<String, List> cdCatalogue;
   
   /**
    * Constructor for objects of MusicLibrary.
    */
   
   public MusicLibrary()
   {
     
      super();
      this.cdCatalogue  = new TreeMap<String, List>();
   }
   
   
   
   
   public void displayCDs()
   {
      
      for (String eachArtist : cdCatalogue.keySet())
         {
            System.out.println (eachArtist + cdCatalogue.get(eachArtist));
         }

  }
   
   
         
   /**
    * Add some CDs to a music library - provided by the course team to help in testing 
    * student methods. 
    * 
    * Studying this method may give you some ideas for the rest of the question 
    * but it does not add the CDs in the way required by part (iv) of the question.
    */
   public void populate()
   {
      List<CD> temp;
      temp = new ArrayList<CD>();
      temp.add(new CD("Andy Sheppard","Soft on the Inside", "Jazz",1990));
      this.cdCatalogue.put("Andy Sheppard", temp);
      temp = new ArrayList<CD>();
      temp.add(new CD("Barbara Thompson","Songs from the Centre of the Earth","jazz",1991));
      this.cdCatalogue.put("Barbara Thompson", temp);
      temp = new ArrayList<CD>();
      temp.add(new CD("Benjamin Britten", "Piano Concerto", "Classical",1938));
      this.cdCatalogue.put("Benjamin Britten", temp);
      temp = new ArrayList<CD>();
      temp.add(new CD("Bob Dylan","Blood on the Tracks","Other", 1975));
      this.cdCatalogue.put("Bob Dylan", temp);
      this.cdCatalogue.get("Bob Dylan").add(new CD("Bob Dylan","No Direction Home", "Folk/rock",2005));
      this.cdCatalogue.get("Bob Dylan").add(new CD("Bob Dylan","Street Legal", "Folk/rock", 1978));
      temp = new ArrayList<CD>();
      this.cdCatalogue.get("Bob Dylan").add(new CD("Bob Dylan", "Blonde on Blonde", "Folk/rock", 1966));
      temp = new ArrayList<CD>();
      temp.add(new CD("Dave Brubeck Quarter", "Time Out", "Jazz",1959));
      this.cdCatalogue.put("Dave Brubeck Quartet", temp);
      temp = new ArrayList<CD>();
      temp.add(new CD("Gilad Atzmon", "Rearranging the 20th Century","Jazz",2004));
      this.cdCatalogue.put("Gilad Atzmom", temp);
      temp = new ArrayList<CD>();
      temp.add(new CD("Inti-Illimani", "Leyenda", "World music", 1990));
      this.cdCatalogue.put("Inti-Illimani", temp);
      temp = new ArrayList<CD>();
      temp.add(new CD("John Adams","Shaker Loops","New music",1983));
      this.cdCatalogue.put("John Adams",temp);
      temp = new ArrayList<CD>();
      temp.add(new CD("Louis Sclavis", "Les Violences de Rameau", "Jazz",1996));
      this.cdCatalogue.put("Louis Sclavis", temp);
      this.cdCatalogue.get("Louis Sclavis").add(new CD("LouisSclavis","Clarinettes","Jazz",1984));
      this.cdCatalogue.get("Louis Sclavis").add(new CD("Louis Sclavis", "Phare", "Jazz", 2005));
      temp = new ArrayList<CD>();
      temp.add(new CD("Miles Davies", "Porgy and Bess", "Jazz", 1958));
      this.cdCatalogue.put("Miles Davies", temp);
      temp = new ArrayList<CD>();
      temp.add(new CD("Mozart","Sinfonia Concertante","Classical",1778));
      this.cdCatalogue.put("Mozart", temp);
      this.cdCatalogue.get("Mozart").add(new CD("Mozart","The Magic Flute","Classical",1791));
      this.cdCatalogue.get("Mozart").add(new CD("Mozart","Violin Concertos","Classical",1775));
      this.cdCatalogue.get("Mozart").add(new CD("Mozart","Clarinet Concerto","Classical",1791));
      temp = new ArrayList<CD>();
      temp.add(new CD("Orchestra Baobab","Bamba", "World music", 1981));
      this.cdCatalogue.put("Orchestra Baobab", temp);
      temp = new ArrayList<CD>();
      temp.add(new CD("The Clash", "The Clash", "Punk", 1977));
      this.cdCatalogue.put("The Clash", temp);
      this.cdCatalogue.get("Andy Sheppard").add(new CD("Andy Sheppard", "Andy Sheppard", "Jazz",1998));
      this.cdCatalogue.get("Andy Sheppard").add(new CD("Andy Sheppard","The Birds", "Jazz", 2006));
      temp = new ArrayList<CD>();
      temp.add(new CD("The Sex Pistols","The Great Rock n Roll Swindle", "Punk", 1979));
      this.cdCatalogue.put("The Sex Pistols",temp);
   }

}

the way I have the displayCDs() method at the minute in MusicLibrary has output like this.

Andy Sheppard[CD@156f920, CD@fdbc27, CD@4be179]
Barbara Thompson[CD@98f9c2]
Benjamin Britten[CD@135707c]
Bob Dylan[CD@16c14e7, CD@d0726d, CD@1087359, CD@1ccf342]
Dave Brubeck Quartet[CD@7691c0]
Gilad Atzmom[CD@5b02a6]
Inti-Illimani[CD@10aefdb]
John Adams[CD@10948bd]
Louis Sclavis[CD@8697ce, CD@586403, CD@10e2610]
Miles Davies[CD@1e4f66a]
Mozart[CD@ede59e, CD@7dedad, CD@17efd36, CD@21e605]
Orchestra Baobab[CD@1845568]
The Clash[CD@1032cf5]
The Sex Pistols[CD@17c8f7f]

But I need it to print the CD details.
I think I may need to use the printString() method that is in the CD class, but I'm not sure how I can do that.
Cheers...

CD@21e605 is what you get from the inherited toString() method - ie object type + pointer. Add a
public String toString()
method in your CD class and what it returns is what will automatically be used when the system wants to print a CD.
You don't really need printString once you've done that, because you can simply print the CD.

I do see what your saying James. The only thing is that the questions want you to write only the methods it asks, it never asked to override toString().
Is there another way to get it to print the actual details? Is there a way ..I can use printString() to do this?
Cheers

This is the standard way to do it. It's universal throughout the API.
Every object in Java inherits toString from Object, and if you look at the code for PrintStream's print and println methods you see that
print(Object o)
is implemented by sending the result of o.toString(); to the PrintStream.

It's not obligatory to override toString, but it's always done.

Once you have done it you can print any CD by just
System.out.println(myCD);

ps toString is also used to display things when you add them to list boxes and other GUI components

Edited 5 Years Ago by JamesCherrill: ps

I have done that, and it works a treat James. One thing though, how would I get each CDs details to display on a separate line? At the minute it's displaying as so;


[Andy Sheppard Soft on the Inside Jazz 1990, Andy Sheppard Andy Sheppard Jazz 1998, Andy Sheppard The Birds Jazz 2006]
[Barbara Thompson Songs from the Centre of the Earth jazz 1991]
[Benjamin Britten Piano Concerto Classical 1938]
[Bob Dylan Blood on the Tracks Other 1975, Bob Dylan No Direction Home Folk/rock 2005, Bob Dylan Street Legal Folk/rock 1978, Bob Dylan Blonde on Blonde Folk/rock 1966]
[Dave Brubeck Quarter Time Out Jazz 1959]
[Gilad Atzmon Rearranging the 20th Century Jazz 2004]
[Inti-Illimani Leyenda World music 1990]
[John Adams Shaker Loops New music 1983]
[Louis Sclavis Les Violences de Rameau Jazz 1996, LouisSclavis Clarinettes Jazz 1984, Louis Sclavis Phare Jazz 2005]
[Miles Davies Porgy and Bess Jazz 1958]
[Mozart Sinfonia Concertante Classical 1778, Mozart The Magic Flute Classical 1791, Mozart Violin Concertos Classical 1775, Mozart Clarinet Concerto Classical 1791]
[Orchestra Baobab Bamba World music 1981]
[The Clash The Clash Punk 1977]
[The Sex Pistols The Great Rock n Roll Swindle Punk 1979]

Thanks for the help.........

That's just the default way of printing for a Map<String, List>
If you want to format it differently you can write your own little nested loop thing that prints Map<String, List> however you like. Or (bit of a fudge this) embed a \n in the String returned by your CD toString method.

This question has already been answered. Start a new discussion instead.