Reputation: 1
Here's my Java Code:
public class DVD {
public static void main(String[] args) {
DVD newdvd1 = new DVD();
newdvd1.setPlayit("The song is playing \n");
newdvd1.setArtist("Eva Cassidy");
newdvd1.setTitle("Songbird");
newdvd1.setGenre("Blues");
System.out.println(newdvd1.getPlayit());
System.out.println(newdvd1);
DVD newdvd2 = new DVD();
newdvd2.setPlayit("The next song is playing \n");
newdvd2.setArtist("an unknown artist");
newdvd2.setTitle("new song");
System.out.println(newdvd2.getPlayit());
System.out.println(newdvd2);
}
private String artist;
private String title;
private String genre;
private String playit;
public String getPlayit() {
return playit;
}
public void setPlayit(String playit) {
this.playit = playit;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public String toString () {
return ("The artist is called " + artist +
" who is a "+ genre + " singer" +
" and this song is called " + title + ".\n");
}
}
what it outputs is:
The song is playing
The artist is called Eva Cassidy who is a Blues singer and this song is called Songbird.
The next song is playing
The artist is called an unknown artist who is a null singer and this song is called new song.
What i'm asking is, in the second song, how do i leave out 'null singer' as I don't want to display the second songs genre?
Upvotes: 0
Views: 114
Reputation: 15758
Use the ?:
(ternary) operator. See JLS specs, for example.
return ("The artist" +
(artist == null ? "" : " is called " + artist) +
(genre == null ? "" : " who is a "+ genre + " singer") +
(title == null ? "" : " and this song is called " + title) +
".\n");
If the given property is not null, you print the relevant text, otherwise simply print nothing.
You can also define sensible default values like:
(artist == null ? " is unknown" : "is called " + artist) +
Upvotes: 2
Reputation: 416
You can insert a null check to skip this part. Anyway you should use a StringBuffer for that otherwise you are creating many String objects and make the GC angry. Every + creates a new String Object, so with a StringBuffer you only create one String at the end.
public String toString ()
{
StringBuffer buffer = new StringBuffer("The artist is called ");
buffer.append(artist);
if(genre != null)
{
buffer.append(" who is a ");
buffer.append(genre);
buffer.append(" singer");
}
buffer.append(" and this song is called ");
buffer.append(title);
buffer.append(".\n");
return buffer.toString();
}
Upvotes: 0
Reputation: 1316
If its just about the singer, you could do the following:
public String toString() {
String temp = "The artist is called " + artist";
if(singer != null){ //checks if the object "singer" is not null
temp+=" who is a " + genre + " singer";
}
temp+="and this song is called "+ title + ".\n";
return temp;
}
Upvotes: 0
Reputation: 6865
You can try this.
public String toString() {
StringBuilder builder = new StringBuilder();
if (artist != null && !artist.isEmpty()) {
builder.append("The artist is called : " + artist);
}
if (genre != null && !genre.isEmpty()) {
builder.append(" who is a " + genre + " singer");
}
if (title != null && !title.isEmpty()) {
builder.append(" and this song is called " + title + ".\n");
}
return (builder.toString());
}
Upvotes: 1
Reputation: 35577
You should override toString()
using some condition
public String toString () {
if(genre!=null){
return ("The artist is called " + artist +
" who is a "+ genre + " singer" +
" and this song is called " + title + ".\n");
}else{
return ("The artist is called " + artist +
" and this song is called " + title + ".\n");
}
}
Upvotes: 0
Reputation: 68715
If you want the solution to be generic for then modify your toString
method as:
public String toString () {
String returnString = "The artist is called " + artist ;
if(genre !=null && !"".equals(genre.trim())) {
returnString += " who is a "+ genre + " singer" +;
}
returnString += " and this song is called " + title + ".\n";
return returnString;
}
You can add more null checks if required for artist and title as well.
Upvotes: 0