2014-09-25 28 views
0

这里是我的Java代码:如何留下了一个空值在一个循环中的Java

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"); 
    } 
} 

它所输出的是:

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. 

什么我问的是,在第二首歌曲,因为我不想显示第二首歌曲类型,我该如何离开'空歌手'?

+2

'如果(东西== NULL){doSomethingSpecial(); }' – Thilo 2014-09-25 11:48:41

回答

0

如果你想解决方案是通用的,然后修改toString方法:

  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; 
     } 

如果需要艺术家和标题,以及您可以添加更多null检查。

0

您可以插入空检查以跳过此部分。无论如何,你应该使用一个StringBuffer,否则你会创建许多String对象,并使GC生气。 Every +创建一个新的字符串对象,因此对于一个StringBuffer,最后只能创建一个字符串。

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(); 
} 
2

使用?:(三元)运算符。例如,请参阅JLS specs

    return ("The artist" + 
        (artist == null ? "" : " is called " + artist) + 
        (genre == null ? "" : " who is a "+ genre + " singer") + 
        (title == null ? "" : " and this song is called " + title) + 
        ".\n"); 

如果给定的属性不为空,则打印相关文本,否则仅打印任何内容。

您还可以定义合理的默认值,如:

    (artist == null ? " is unknown" : "is called " + artist) + 
+3

我认为值得注意的是'?:'运算符被称为'三元运算符'。 – Mister 2014-09-25 12:00:46

0

你应该使用一些条件

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");   
    } 

} 
+0

BOOM!我们在行动中,非常感谢你 – joewinfield91 2014-09-25 12:00:34

+0

@ joewinfield91欢迎您 – 2014-09-25 12:01:46

1

你可以试试这个覆盖toString()

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()); 
} 
0

如果它只是歌手,你可以做到以下几点:

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; 
} 
相关问题