0

enter image description here

package PlaylistForSongs;

import java.util.ArrayList; import java.util.Scanner;

public class Main {

private static ArrayList<Album> listOfAlbums = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {

    Album album = new Album("Fruit du démon", "Soolking");
    album.addSong("Milano", 400);
    album.addSong("Guerilla", 433);
    album.addSong("Dalida", 233);
    album.addSong("Liberté", 323);
    album.addSong("Zemër", 432);
    album.addSong("Espérance", 123);
    listOfAlbums.add(album);

    album = new Album("Dellali", "Cheb Mami");
    album.addSong("Le Raï c'est Chic", 300);
    album.addSong("Ana Oualache", 533);
    album.addSong("Zarartou", 433);
    album.addSong("Khalouni", 423);
    album.addSong("Ma Vie 2 Fois", 432);
    album.addSong("Machi Chaba", 123);
    album.addSong("Chaba", 1223);
    listOfAlbums.add(album);

    PlayList myFavorite = new PlayList("My favorite", listOfAlbums);
    System.out.println("**************************************************************************");
    myFavorite.addSong("Machi Chaba");
    myFavorite.addSong("Zemër");
    myFavorite.addSong("Ana Oualache");
    myFavorite.addSong("ntia Machi Chaba");

    boolean quit = false;
    printChoice();
    System.out.print("your choice is: ");

    while (!quit) {
        int choice = scanner.nextInt();
        scanner.nextLine();
        switch (choice - 1) {
            case 0:
                quit = true;
                System.out.println("closing the play list ...");
                break;
            case 1:
                myFavorite.playNext();
                break;
            case 2:
                myFavorite.playBackward();
                break;
            case 3:
                myFavorite.replaySong();
                break;
            case 4:
                printChoice();
                break;
        }


    }
}

private static void printAlbums() {
    System.out.println("\nAlbums:\n************************");
    for (Album a : listOfAlbums) {
        System.out.println(a.getName());
    }
    System.out.println("\n************************");
}

public static void printChoice() {
    System.out.println("choices: \n" +
            "1- Quit\n" +
            "2- Skip forward to the next song\n" +
            "3- skip backwards to a previous song.\n" +
            "4- Replay the current song.\n" +
            "5- Print Choices");
}

}

package PlaylistForSongs;

public class Song {

private String title;
private int duration;

public Song(String title, int duration) {
    this.title = title;
    this.duration = duration;
}

public String getTitle() {
    return title;
}

public String getDuration() {

    int hours;
    int minutes;
    int seconds;

    seconds = duration;
    minutes = seconds / 60;
    seconds %= 60;
    if( minutes > 60 ){
        hours = minutes / 60;
        minutes %= 60;
    }else{
        hours = 0;
    }

    return hours + " : " + minutes + " : " + seconds;
}

@Override
public String toString() {
    return this.title + ": " + this.getDuration();
}

}

package PlaylistForSongs;

import java.util.ArrayList;

public class Album {

private ArrayList<Song> songsList;
private String name;
private String artist;
private int numberOfSongs;


public Album(String name, String artist) {
    this.songsList = new ArrayList<>();
    this.name = name;
    this.artist = artist;
    this.numberOfSongs = 0;
}

public String getName() {
    return name;
}

public int getNumberOfSongs() {
    return numberOfSongs;
}

public boolean addSong( Song song ){
    for ( Song s : this.songsList ) {
        if( s.equals(song) ){
            System.out.println("The song " + song + " Already exist in the album" + this.name);
            return false;
        }
    }
    songsList.add(song);
    System.out.println("The song " + song + " added successfully in " + this.name);
    this.numberOfSongs ++;
    return true;
}

public boolean addSong( String title, int duration){
    for( Song s : this.songsList){
        if( s.getTitle().equals(title)){
            System.out.println("The song " + title + " Already exist in the album" + this.name);
            return false;
        }
    }
    songsList.add(new Song(title, duration));
    System.out.println("The song " + title + " added successfully in " + this.name);
    this.numberOfSongs ++;
    return true;
}

public void show(){
    System.out.println("\nThe album " + this.name + " has " + this.numberOfSongs + "\nThe songs: ");
    for( Song s : this.songsList){
        System.out.println(s.toString());
    }
}

public Song getSong( String Title){
    for( int i = 0; i < songsList.size(); i++){
        if(songsList.get(i).getTitle().equals(Title)){
            return songsList.get(i);
        }
    }
    System.out.println("the song " + Title + " doesn't exist in album " + this.name + " from album");
    return null;
}

public boolean checkSong( String songTitle){
    for( int i = 0; i < songsList.size(); i++){
        if(songsList.get(i).getTitle().equals(songTitle)){
            return true;
        }
    }
    return false;
}
private boolean checkSong( Song song){
    for( int i = 0; i < songsList.size(); i++){
        if(songsList.get(i).getTitle().equals(song.getTitle())){
            return true;
        }
    }
    return false;
}

}

package PlaylistForSongs;

import java.util.*;

public class PlayList {

private String name;
private LinkedList<Song> songsInPlay;
private ArrayList<Album> albumList;
private boolean forward;
private ListIterator<Song> songsInPlayIterator ;

public PlayList(String name, ArrayList<Album> albumList) {
    this.name = name;
    this.songsInPlay = new LinkedList<>();
    this.albumList = albumList;
    this.forward = true;
    songsInPlayIterator = songsInPlay.listIterator();
}

public void addSong(String title) {
    if (findSong(title) != null) {
        this.songsInPlay.add(findSong(title));
        System.out.println("The song " + title + " has been added successfully to the Play List: " + name);
    } else {
        System.out.println("we can't add the song " + title + " it doesn't exist in our albums");
    }
}

private Song findSong(String songName) {
    for (Album album : albumList) {
       if( album.checkSong(songName)){
            return album.getSong(songName);
        }
    }
    return null;
}

public void playNext(){

    if(songsInPlayIterator.hasNext()){
        System.out.println("the song: " + songsInPlayIterator.next() + "in play");
        this.forward = true;
    }
}

public void playBackward(){
    if(songsInPlayIterator.hasPrevious() && forward){
        songsInPlayIterator.previous();
        forward = false;
    }
    System.out.println("the song: " + songsInPlayIterator.previous() + "in play");
}

public void replaySong(){
    if(songsInPlayIterator.hasPrevious() && forward){
        System.out.println("the song: " + songsInPlayIterator.previous() + "in play");
        forward = false;
    }
    if (songsInPlayIterator.hasNext() && !forward){
        System.out.println("the song: " + songsInPlayIterator.next() + "in play");
        forward = true;
    }
}

this is the ERROR Exception in thread "main" java.util.ConcurrentModificationException at java.base/java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:970) at java.base/java.util.LinkedList$ListItr.next(LinkedList.java:892) at PlaylistForSongs.PlayList.playNext(PlayList.java:42) at PlaylistForSongs.Main.main(Main.java:52)

In PlayList introduce the statement

 songsInPlayIterator = songsInPlay.listIterator();

after lines 40, 47, 55 after doing that i get this result enter image description here

  • Which line is `Main.java:52`? – Louis Wasserman Aug 11 '20 at 21:02
  • 2
    ConcurrentModificationException is telling you that your iterator is stale; you can't modify a list and iterate over it at the same time (unless you modified the list it *via* the iterator). In this case, the `songsInPlayIterator` becomes invalid whenever you add a song to the list. – dnault Aug 11 '20 at 21:51
  • @dnault thanks for your explanation but when i add In PlayList the statement songsInPlayIterator = songsInPlay.listIterator(); after lines 40, 47, 55 it work but the iterator start from the begging each time – El Orabi Mohamed Aug 12 '20 at 08:47

0 Answers0