Skip to content

Java – Logic error appending a user input to an ArrayList

I’m writing a program that allows the users to create a custom playlist. I have currently gotten all inputs, assigned them values and placed them in an object.

However, when I try to append this object to an ArrayObject, it only appends int duration, and upon output, it simply prints

Duration: X (with X being the value inputted by the user).

What am I doing wrong here?

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

class Track {
    public String title, artist, genre;
    public int duration;
    
    public Track(String title, String artist, String genre, int duration) {
        this.title = title; //ti
        this.artist = artist; //t
        this.genre = genre; //d
        this.duration = duration; //id
    }
    
    public String getTitle() {
        return title;
    }   

    public String getArtist() {
        return artist;
    }
    
    public String getGenre() {
        return genre;
    }
    
    public int getDuration() {
        return duration;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }
    
    public void setGenre(String genre) {
        this.genre = genre;
    }
    
    public void setDuration(int duration) {
        this.duration = duration;
    }
    
    public void printEntry() { 
        System.out.print("Title: "+title+"nArtist: "+artist+"nType: "+genre+"n"+toString());
    }

    public String toString() {
        return "Duration: "+duration;
    }
}
class MyPlaylist {
    private ArrayList<Track> songList;
    private static int songCount = 0;
    public static boolean programActive = true;

    public static void main(String[] args){
        while (programActive) {
            userInterface();
        }
    }
    
    public MyPlaylist() {
        songList = new ArrayList<Track>();
    }
    
    public MyPlaylist(Track entry) {
        songList = new ArrayList<Track>();
        songList.add(entry);
    }
    
    public void addEntry (Track entry) {
        songList.add(entry);
    }   

    public void printList(Track[] songs) {
        for (int i = 0; i < songs.length; i++){
            songs[i].printEntry();
        }
    }

    public Track[] getPlaylist() {
        Track[] l = new Track[songList.size()];
        System.out.println(songList.size());
        
        for(int i = 0; i < l.length; i++){
            l[i] = songList.get(i);
        }
        
        return l;
    }
    
    public void deleteTrack(int trackNum) {
        
    }
    

    // --- MAIN USER INTERFACE ---
    private static void userInterface() {
        MyPlaylist songPlaylist = new MyPlaylist();
        Scanner userInput = new Scanner(System.in);
        
            System.out.println("n1 - Add a new songn2 - List all songsn3 - Delete an existing songn4 - Search for a songn5 - Display total playtimen6 - exitnPlease enter your choice:");
            int input = userInput.nextInt();
        
            switch (input) {
            case 1: 
                Scanner newSongEntry = new Scanner(System.in); 

                System.out.println("nTitle of the song:");
                String title = newSongEntry.nextLine(); 
                
                
                System.out.println("Name of the artist:");
                String artist = newSongEntry.nextLine(); 


                System.out.println("Type of the song:");
                String genre = newSongEntry.nextLine(); 


                System.out.println("Duration (seconds):");
                int duration = newSongEntry.nextInt(); 

                Track newEntry = new Track(title, artist, genre, duration);
                songPlaylist.addEntry(newEntry);

                break;
        
            case 2:
                songPlaylist.printList(songPlaylist.getPlaylist());
                break;
        
            case 3:
                break;
        
            case 4:
                break;
        
            case 5:
                break;

            case 6:
                System.out.println("nThank you. Bye!");
                programActive = false;
        
        }
    }
}

Answer

Since you’re declaring songPlaylist in userInterface(), the playlist is being remade from scratch after every time you perform and action on it.

I would suggest declaring songPlaylist in main() and passing it to userInterface like this:

public static void main(String[] args){
        MyPlaylist songPlaylist = new MyPlaylist();
        while (programActive) {
            userInterface(songPlaylist);
        }
    }

and this paramater for userInterface:

private static void userInterface(MyPlaylist songPlaylist) { ... }

so you can keep the code inside userInterface