-1

So i created 3 class Song class Album class and Main class, Song class title and duration variable, Album class have ArrayList<Song> this class have method addSong(Song song) its return type is boolean fo what this method is doing is if song is already is the list then its not going to add it mean its return false else it will add, So from Main class i am calling this method. BUt the problem is its giving output like this linkedList.practic.task.Song@76fb509a

Song.java

package linkedList.practic.task;

public class Song {
    private String title;
    private String duration;

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }
}

Album.java

package linkedList.practic.task;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Album {
    private ArrayList<Song> songs;

    public Album() {
        songs = new ArrayList<>();
    }

    public boolean addSong(Song song){
        if(!this.songs.contains(song)){
            this.songs.add(song);
            return true;
        }
        return false;
    }

    public void printSongs(){
        Iterator<Song> iterator = songs.iterator();

        while(iterator.hasNext()){
            System.out.println(iterator.next() + " ");
        }
    }
}

Main.java

package linkedList.practic.task;
import java.util.Scanner;

public class Main {
    static Scanner scan = new Scanner(System.in);
    static Album album = new Album();
    public static void main(String[] args) {
        boolean flag = true;

        while(flag){
            System.out.println("Enter your choice");
            int action = scan.nextInt();
            switch (action){
                case 0:
                    flag = false;
                    break;

                case 1:
                    addSong();
                    break;

                case 2:
                    album.printSongs();
            }
        }
    }

    private static void addSong(){
        System.out.println("Enter the name of the song");
        String name = scan.nextLine();
        scan.nextLine();
        System.out.println("Enter the duration of song");
        String duration = scan.nextLine();
        if(album.addSong(new Song(name, duration))){
            System.out.println( name + " is added to the list");
        }
        else{
            System.out.println( name + " is already in the list");
        }

    }

}

also after adding song its not printing name of song its only printing is added to the listand same for the else part

Ayush Verma
  • 103
  • 2
  • 10
  • 2
    Possible duplicate of [How do I print my Java object without getting "SomeType@2f92e0f4"?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Govinda Sakhare Oct 12 '19 at 05:45

5 Answers5

1

u have to override toString method of Song class

hossein rasekhi
  • 209
  • 1
  • 13
0

You need to override toString() method in your Song class as by default implementation present on Object class returns the id of object. Something like this would work for you -

Public String toString(){
      return getTitle() +" " + getDuration;
}
Aviral Ahuja
  • 205
  • 1
  • 2
  • 14
0
    String name = scan.nextLine();
    scan.nextLine();

should change to :

   scan.nextLine();
   String name = scan.nextLine();
Sepideh_j
  • 1
  • 1
0

you need to override hashcode() method and toString() in your song.java class try this one

0

There are few things you need to do to make your code work.

  1. Checking on equality: Your code will add duplicate songs as well. You need to write your own equals method. Check this link for more How to override equals method in Java

  2. You need to override toString method to print objects so that you don't get output like this linkedList.practic.task.Song@76fb509a. Check here for more details.How do I print my Java object without getting "SomeType@2f92e0f4"?

  3. name is not being assigned to the variable in addSong. You can check that by printing it before adding a song.

To make it work check below addSong method:

private static void addSong(){
    System.out.println("Enter the name of the song");
    scan.nextLine();
    String name = scan.nextLine();
    System.out.println("Enter the duration of song");
    String duration = scan.nextLine();
    System.out.println("Name: " + name + "; Duration: " + duration);
    if(album.addSong(new Song(name, duration))){
        System.out.println( name + " is added to the list");
    }
    else{
        System.out.println( name + " is already in the list");
    }
}
Madstuffs
  • 374
  • 4
  • 13