Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28
  1. #21
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Just a couple of suggestions here.
    There's no need to iterate over the keySet then use the keys to get the entries. Just get the entry set (entrySet()) and iterate over that. It's much neater, and functionally does the same thing you're doing.

    So this:
    Java Code:
             for (String eachCD : this.cdCatalogue.keySet())
             {
                List<CD> cdList = this.cdCatalogue.get(eachCD);
    ...
    becomes this:
    Java Code:
             for (List<CD> cdList : this.cdCatalogue.entrySet())
             {
    ...
    Second, isn't the key to your map the artist?
    So why not do this (note, variable names should start with a lower case):
    Java Code:
    List<CD> artistsCD = cdCatalogue.get(anArtist);
    ?
    Thank you for your input. I knew my way was long winded but it was working so I was counting my blessings!
    I have now tried this code and this also works. Once again thanks.
    Java Code:
    public void displayMostRecent(String anArtist)
       {
             List<CD> artistsCD = this.cdCatalogue.get(anArtist);
             if (this.cdCatalogue.keySet().contains(anArtist))
             {
                System.out.println(artistsCD.get(0));
             }
             else
             {
                System.out.println("There are no titles for this artist");
             }
        }

  2. #22
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Quote Originally Posted by trueblue View Post
    I also tried a boolean check on
    Java Code:
    ArtistCDList.isEmpty()
    and I thought this would work but it still threw an exception?
    OK, so what exception, at what line?
    Might be an idea to give us the changed bit of code as well.

    I'm assuming you're now doing:

    Java Code:
    if (!artistCDList.isEmpty()) {
      System.out.println(ArtistCDList.get(0));
    } else {
      System.out.println("There are no titles for this artist");
    }
    or something to that effect anyway.

    Oh, and no probs on the suggestions...code reviews and all that.
    :)

  3. #23
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Quote Originally Posted by trueblue View Post
    Thank you for your input. I knew my way was long winded but it was working so I was counting my blessings!
    I have now tried this code and this also works. Once again thanks.
    Java Code:
    public void displayMostRecent(String anArtist)
       {
             List<CD> artistsCD = this.cdCatalogue.get(anArtist);
             if (this.cdCatalogue.keySet().contains(anArtist))
             {
                System.out.println(artistsCD.get(0));
             }
             else
             {
                System.out.println("There are no titles for this artist");
             }
        }
    Much neater...:)

    However, since you've already go the list it's probably better to simply see if artistsCD isn't null (ie the artist exists) and (just a belt and braces thing) that artistsCD isn't empty (it's possible to have an empty cd list, at least theoretically). No need to go back to the catalogue key set.

  4. #24
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    OK, so what exception, at what line?
    Might be an idea to give us the changed bit of code as well.

    I'm assuming you're now doing:

    Java Code:
    if (!artistCDList.isEmpty()) {
      System.out.println(ArtistCDList.get(0));
    } else {
      System.out.println("There are no titles for this artist");
    }
    or something to that effect anyway.

    Oh, and no probs on the suggestions...code reviews and all that.
    :)
    This code gives a java.lang.NullPointerException exception when the artist does NOT exist....

    Java Code:
    public void displayMostRecent(String anArtist)
       {
             List<CD> artistsCD = this.cdCatalogue.get(anArtist);
             if (!artistsCD.isEmpty())
             {
                System.out.println(artistsCD.get(0));
             }
             else
             {
                System.out.println("There are no titles for this artist");
             }
        }

  5. #25
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    So put an if check for null as well
    Java Code:
    if (artistsCD!= null && (!artistsCD.isEmpty()))
             {
                System.out.println(artistsCD.get(0));
             }
    ....

  6. #26
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    So put an if check for null as well
    Java Code:
    if (artistsCD!= null && (!artistsCD.isEmpty()))
             {
                System.out.println(artistsCD.get(0));
             }
    ....
    Yes thanks that works. I was surprised that null check was needed under the circumstances, but java is always there to fill me with a sense of bewilderment :confused:

  7. #27
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    When you think about it a bit more it should get less surprising.
    You have a cdCatalogue and are calling cdCatalogue .get(anArtist).
    What if the String represented by anArtist is not in the cdCatalogue?
    e.g if I call cdCatalogue .get("r035198x"); on your list then the best Java can do is to return null, because r035198x is not a valid artist. Certainly not in your cdCatalogue.

  8. #28
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Quote Originally Posted by r035198x View Post
    So put an if check for null as well
    Java Code:
    if (artistsCD!= null && (!artistsCD.isEmpty()))
             {
                System.out.println(artistsCD.get(0));
             }
    ....
    Bah!
    That's what comes from coding on the fly...:)

    ETA: Hang about...I did say check for null.
    I really must have more confidence in my, sometimes superhuman, abilities...;)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 8
    Last Post: 12-29-2008, 07:12 AM
  2. Capture elements from an List in a different package
    By blevault in forum New To Java
    Replies: 5
    Last Post: 11-27-2008, 09:14 AM
  3. trying to set() values of in list of arraylist
    By alvations in forum New To Java
    Replies: 15
    Last Post: 10-13-2008, 09:35 PM
  4. Getting values of an Option List
    By mutuah in forum Advanced Java
    Replies: 0
    Last Post: 08-07-2007, 03:42 AM

Posting Permissions

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