Desert Island Playlist

Hi!

First of all, let me thank you for all the invaluable help and encouragement you give me on here each time I get stuck with my code.

This time, I’ve run into some difficulty while working through the ‘Desert Island Project’.

After successfully completing the first (easier) part of the unit, I started doing the tricky swapping exercise, and when I tried to run the fresh block of code, it wouldn’t run. I cleared the terminal and still no result.

That’s the point where you are supposed to see the same song listed twice in the ArrayList, after moving songB to the Index location of songA using the set() method.

I even watched the video walkthrough but the steps the developper took us through were exactly the same as mine.

I’m still racking my brains about where I could have gone wrong. It must be something obvious again I overlooked!

I’ll copypaste my code here below (sorry about the Italian titles, they are pieces of classical music I love)


import java.util.ArrayList;

class Playlist {

  

  public static void main(String[] args) {

ArrayList<String> desertIslandPlaylist = new ArrayList<String>();

desertIslandPlaylist.add("Rompo i lacci!");

desertIslandPlaylist.add("Toccata e fuga");

desertIslandPlaylist.add("Sorge talora");

desertIslandPlaylist.add("Torbido intorno al cuore");

desertIslandPlaylist.add("Erfteute Ich, Herzen");

desertIslandPlaylist.add("Water Music");

desertIslandPlaylist.add("Music for the Royal Fireworks");   

 System.out.println(desertIslandPlaylist); 

 System.out.println(desertIslandPlaylist.size()); 

desertIslandPlaylist.remove(2);

desertIslandPlaylist.remove(4); 

System.out.println(desertIslandPlaylist.size()); 

System.out.println(desertIslandPlaylist); 


int indexA = desertIslandPLaylist.indexOf("Toccata e fuga");

int indexB = desertIslandPlaylist.indexOf("Torbido intorno al cuore");

String tempA = "Toccata e fuga";    

desertIslandPlaylist.set(indexA, "Torbido intorno al cuore");

System.out.println(desertIslandPlaylist.size()); 

System.out.println(desertIslandPlaylist); 

  }
}

I ran your code, and received a very useful error message. Did you get an error message when you ran the code?

I would expect that you did. If so, read it carefully. If you can’t make sense of it, copy it, and post it in a reply.

No, I didn’t get any error message this time around.That’s the whole point, Whenever I get one I can usually figure it out on my own as these warnings are exremely helpful; they point you in the right direction (e.g. ‘reached the end of file while parsing’ means an incorrect number of curly braces at the end of the code) But this time it all went mute, no response.

So I am surprised you received an error message at all running the exact same code. What is it, if I may ask you? Or if you want me to make an effort, which is totally acceptable, just give me a hint and I will mull over it,

Thanks a lot!

Pat

1 Like

The error referenced this line. Not the exact wording but desertIslandPLaylist is undefined.

Edit: Here’s the error from running your code on pythontutor.com. The site has a great visualizer for code in several languages.

The code seemed to function as expected after fixing the above.

Oh, okay, thank you! I fixed it this morning and now the code is running!

I knew there was a typo somewhere… !

Now I understand why it said it was ‘undefined’:)!

Pat

1 Like