Avatar of Software Software
Software Software
Flag for Austria asked on

How to fix "IllegalStateException" in my code

Hello,
The program is about playing the next or previous song, replay or remove a song.
If I want to delete a song immediately after starting the program (case 5), I get an "IllegalStateException".
How do I fix the error?
If you see further errors or have improvements to the code, then you can also tell me.


import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Scanner;

public class Songs {

    public static void main(String[] args) {

        LinkedList<String> songs = new LinkedList<>();
        songs.add("Song 1");
        songs.add("Song 2");
        songs.add("Song 3");
        songs.add("Song 4");
        songs.add("Song 5");
        songs.add("Song 6");

        Scanner scanner = new Scanner(System.in);
        boolean quit=false;
        boolean forward=true;
        ListIterator currentSong = songs.listIterator();

        printMenu();
        while(quit==false){
            switch(scanner.nextInt()){
                case 1:
                    printMenu();
                    break;
                case 2:
                    if(!forward){
                        if(currentSong.hasNext()){
                            currentSong.next();
                        }
                        forward=true;
                    }
                    if(currentSong.hasNext()){
                        System.out.println("Now playing " + currentSong.next());
                    }
                    else{
                        System.out.println("Reached the end of the playlist");
                    }
                    break;
                case 3:
                    if(forward){
                        if(currentSong.hasPrevious()){
                            currentSong.previous();
                        }
                        forward=false;
                    }
                    if(currentSong.hasPrevious()){
                        System.out.println("Now playing " + currentSong.previous());
                    }
                    else{
                        System.out.println("We are at the start of the playlist");
                    }
                    break;
                case 4:
                    if(currentSong.hasPrevious()){
                        currentSong.previous();
                        System.out.println("Replaying " + currentSong.next());
                    }
                    else if(currentSong.hasNext()){
                        currentSong.next();
                        System.out.println("Replaying " + currentSong.previous());
                    }
                    break;
                case 5:
                    if(songs.size()>0){
                        currentSong.remove();
                        if(currentSong.hasNext()){
                            currentSong.next();
                        }
                        else if(currentSong.hasPrevious()){
                            currentSong.previous();
                        }
                    }
                    break;
                case 6:
                    for(String song: songs){
                        System.out.println(song);
                    }
                    break;
                case 7:
                    quit=true;
                    break;
                default:
                    printMenu();
                    break;
            }
        }
    }

    private static void printMenu(){
        System.out.println("1 - Print Menu\n" +
                           "2 - Go to the next song\n" +
                           "3 - Go to previous song\n" +
                           "4 - Replay current song\n" +
                           "5 - Remove current song\n" +
                           "6 - Print all songs\n" +
                           "7 - Quit\n" +
                           "============================");
    }
}

Open in new window

ProgrammingJavaAlgorithms

Avatar of undefined
Last Comment
krakatoa

8/22/2022 - Mon
noci

you probably need to remove the song AFTER moving to the next.
Arana (G.P.)

listiterator starts at index (0)

you cannot remove an item that is not there yet

call currentSong.next()  first, then call currentSong.remove();
ASKER CERTIFIED SOLUTION
Sathish David Kumar N

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
krakatoa

Regarding menu choice 4 (replaying current song), of course there won't really be a current song " to replay" at the very start of the programme, so you might want to give that situation some more thought. Nor can it be entirely clear whether the "current" song" should refer to the one that was last played, or to the one that the user has navigated to by choosing a different one with the previous and next choices, 2 and 3.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
krakatoa

I posted code to this question - anyone know what happened to it?