-4

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;
Lrrr
  • 4,755
  • 5
  • 41
  • 63
camohreally
  • 149
  • 3
  • 4
  • 17
  • See this: http://stackoverflow.com/q/7520432/10077 – Fred Larson May 30 '15 at 04:26
  • http://stackoverflow.com/questions/13387742/compare-two-objects-with-equals-and-operator – vijayalakshmi d May 30 '15 at 04:26
  • @cameron first decide on which basis you want to say that your two song objects are equal. according to which you have to override your equals and hash method for song. thanx..hope it may help you. – Avyaan May 30 '15 at 05:05

2 Answers2

0
if (newSong.equals(database.getSong(0))){
               throw new Exception("Exception: duplicate song entry!");

From Oracle Doc:

"public boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

It is reflexive: for any non-null reference value x, x.equals(x) should return true.
It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
For any non-null reference value x, x.equals(null) should return false. 

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

Parameters: obj - the reference object with which to compare. Returns: true if this object is the same as the obj argument; false otherwise."

Mirodinho
  • 1,171
  • 1
  • 13
  • 25
0

== works for primitive types, like int , long , ... if you want to compare objects of non primitive type you have to override equals method. equal could be override like this:

@Override
public boolean equals(Object sng) {
   if (!(sng instanceof Song))
        return false;
    if (sng == this)
        return true;

    Song otherSong = (Song) sng ;

    //check your equality between this two here.
}

and you could use it in your function like this:

if(song1.equals(song2)){ .... }
Lrrr
  • 4,755
  • 5
  • 41
  • 63