Mit einer LinkedList vorwärts und rückwärts iterieren

software

Mitglied
Hallo,
bei diesem Programm soll der User bestimmen, ob er zur nächten oder zur vorherigen Stadt gehen will.
Wenn ich rückwärts oder vorwärts gehe, dann klappt das erst beim zweiten Mal(siehe Screenshot).
Woran liegt das?

Unbenannt.PNG

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

public class VisitedCities {

    public static void main(String[] args) {

        LinkedList<String> visitedCities = new LinkedList<>();
        visitedCities.add("Berlin");
        visitedCities.add("Hamburg");
        visitedCities.add("Bremen");
        visitedCities.add("Frankfurt");
        visitedCities.add("Stuttgart");
        visitedCities.add("München");

        Scanner scanner = new Scanner(System.in);
        boolean quit=false;
        ListIterator cities = visitedCities.listIterator();

        printMenu();
        while(quit==false){
            switch(scanner.nextInt()){
                case 1:
                    printMenu();
                    break;
                case 2:
                    if(cities.hasNext()){
                        System.out.println("Now visiting " + cities.next());
                    }
                    else{
                        System.out.println("Reached the end of the journey");
                    }
                    break;
                case 3:
                    if(cities.hasPrevious()){
                        System.out.println("Now visiting " + cities.previous());
                    }
                    else{
                        System.out.println("We are at the start of the journey");
                    }
                    break;
                case 4:
                    quit=true;
                    break;
                default:
                    printMenu();
                    break;
            }
        }
    }

    private static void printMenu(){
        System.out.println("1 - Print Menu\n" +
                           "2 - Go to the next city\n" +
                           "3 - Go to previous city\n" +
                           "4 - Quit\n" +
                           "============================");
    }
}
 
Hi,
ich habe das Programm etwas umgeändert und erweitert.
Es geht darum, den nächsten oder vorherigen Song abzuspielen.
Einen Fehler habe ich noch. Wenn ich direkt nach Programmstart, ein Song löschen will(case 5), dann kommt es zur "IllegalStateException".
Wie behebe ich den Fehler?
Falls, ihr weitere Fehler seht oder Verbesserungen zum Code habt, dann könnt ihr mir diese auch gerne mitteilen.

Java:
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" +
                           "============================");
    }
}
 
Java:
	public static final <T> void backwards(List<T> li, Consumer<T> co) {
		if (co == null) {
			backwards(li.iterator(), System.out::println);
		} else {
			backwards(li.iterator(), co);
		}
	}

	private static final <T> void backwards(Iterator<T> it, Consumer<T> co) {
		if (it.hasNext()) {
			T t = it.next();
			backwards(it, co);
			co.accept(t);
		}
	}

	public static void main(String[] args) {
		LinkedList<String> bla = new LinkedList<String>();
		bla.add("1");
		bla.add("22");
		bla.add("333");
		backwards(bla, null);
	}
 

Neue Themen


Zurück
Oben