Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I override an object that is being passed into a method to string?

It’s exactly as the title says, I am trying to pass an object into a method, so that I can compare strings. The object doesn’t convert properly, showing up as "MediaItem@9a2", and I know I have to use override but I’m not quite sure how to do this.

public class MediaItem {                                                
private String title;                                               
                                            
public String getTitle() {                                              
    return title;                                               
}                                               
                                            
public void setTitle(String title) {                                                
    this.title = title;                                             
}                                               
public MediaItem(String t)                                              
{                                               
    setTitle(t);                                                
}                                               
                                            
public MediaItem()                                              
{                                               
                                            
}                                               
                                            
public boolean equals(Object obj)                                               
{                                               
String temptitle = getTitle();                                              
temptitle = temptitle.toLowerCase();                                                
temptitle = temptitle.trim();                                               
//    System.out.println(temptitle);                                                
                                            
                                            
String tempobj = obj.toString();                                                
System.out.println(tempobj);                                                
tempobj=tempobj.toLowerCase();                                              
tempobj= tempobj.trim();                                                
System.out.println(tempobj);                                                
                                            
                                            
if (temptitle.equals(tempobj))                                              
{                                               
System.out.println("this");                                             
return true;                                                
}                                               
else{                                               
return false;                                               
}                                               
                                            
}                                               
                                            
@Override                                               
public String toString()                                                
{                                               
return obj.title;                                               
}                                               
                                            
}

There’s a bad attempt at overriding at the bottom of the code. Also, not sure why all the colour has disappeared from the code.

Edit: Ok maybe I should add some more explanation: The task is to compare the obj sent in and the title of the current object. If they match, I return true. If not, I return false. The only thing I am allowed to change is the stuff within the equals(object obj) method. I can also add override. That’s it. Is there an easier way to do this than what I was trying to do?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

public String toString()                                                
{                                               
     return title;                                               
}                   

is sufficient you could also use return this.title; however the this is implicit as there are no other title defined in the method

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading