vendredi 29 mai 2015

Java: values are the same but if statement to check if they're equal won't work?

i'm trying to see if two Song() objects are the same to eventually throw a duplicate song entry Exception but i've encountered some troubles. The if statement to check if they're equal just does not want to accept them as equal. I made the console print their values on each song entry as a test and they were printing as the same but the if statement was not initializing. Here is my code:

            case 1: //ENTER A SONG TO DATABASE

            try {
            Song newSong = enterSong();
            int logSize = database.getLogicalSize();
            if (logSize>0){
                System.out.println(newSong.getName() + " " + database.getSongName(0));
                if (newSong == database.getSong(0)){
                   throw new Exception("Exception: duplicate song entry!");
    }
    }
                database.addSong(newSong);
            }
            catch (Exception e){
                System.out.println(e.getMessage());
            }

            break;

Aucun commentaire:

Enregistrer un commentaire