即使为变量赋值后,变量仍为空 (Java)

Variables are null even after assigning a value to them (Java)

我正在尝试编写一种将歌曲信息(由用户输入)添加到数据库的方法。一旦用户添加了歌曲的详细信息(名称、艺术家、文件大小、持续时间),它们应该被保存在第一个空的歌曲槽(总共 4 个歌曲槽)中,然后被带回菜单界面。

但是当我尝试添加第二首歌曲时,第一个插槽始终是空的,就好像用户输入的详细信息没有保存一样。我试过调试器,当我输入名称、艺术家等时,它们都保存在 song1 对象中。但是当我返回输入第二首歌曲时,song1 对象具有以下值: 名称 = null,艺术家 = null,文件大小 = 0,持续时间 = 0。

我已经在这几个小时了,我真的很困惑,任何帮助都会很棒!

歌曲数据库class:

public class SongDatabase {

Scanner console = new Scanner(System.in);

public void addNewSong() {

    Song song1 = new Song();
    Song song2 = new Song();
    Song song3 = new Song();
    Song song4 = new Song();


if (song1.isEmpty()) {        
    System.out.println("Name of song:");
    song1.setName(console.next());

    System.out.println("Artist:");
    song1.setArtist(console.next());

    System.out.println("File size (MB):");
    song1.setFileSize(console.nextInt());

    System.out.println("Duration (seconds):");
    song1.setDuration(console.nextInt());
    System.out.println("Song successfully added.");
    System.out.println("");
}

else if (song2.isEmpty()) {        
    System.out.println("Name of song:");
    song2.setName(console.next());

    System.out.println("Artist:");
    song2.setArtist(console.next());

    System.out.println("File size (MB):");
    song2.setFileSize(console.nextInt());

    System.out.println("Duration (seconds):");
    song2.setDuration(console.nextInt());
    System.out.println("Song successfully added.");
    System.out.println("");
}

else if (song3.isEmpty()) {        
    System.out.println("Name of song:");
    song3.setName(console.next());

    System.out.println("Artist:");
    song3.setArtist(console.next());

    System.out.println("File size (MB):");
    song3.setFileSize(console.nextInt());

    System.out.println("Duration (seconds):");
    song3.setDuration(console.nextInt());
    System.out.println("Song successfully added.");
    System.out.println("");
}

else if (song4.isEmpty()) {        
    System.out.println("Name of song:");
    song4.setName(console.next());

    System.out.println("Artist:");
    song4.setArtist(console.next());

    System.out.println("File size (MB):");
    song4.setFileSize(console.nextInt());

    System.out.println("Duration (seconds):");
    song4.setDuration(console.nextInt());
    System.out.println("Song successfully added.");
    System.out.println("");
}

else {
    System.out.println("The database is currently full. Please delete a song before adding a new one.");
}

歌曲class:

public class Song {

private String name, artist;
private int fileSize, duration;

public Song(String name, String artist, int fileSize, int duration) {
    name = "";
    artist = "";
    fileSize = 0;
    duration = 0;
}

public Song(){}

public boolean isEmpty() {
    if (this.name == null && this.artist == null && this.fileSize == 0 && this.duration == 0 ) return true;
    else return false;
}

public void setName(String inputName) {
    inputName = name;
}

public String getName() {
    return name;
}

public void setArtist(String inputArtist) {
    artist = inputArtist;
}

public String getArtist() {
    return artist;
}

public void setFileSize(int inputFileSize) {
    if (inputFileSize>0){
        fileSize = inputFileSize;
    }
}

public int getFileSize() {
    return fileSize;
}

public void setDuration(int inputDuration) {
    if (inputDuration>0) {
    duration = inputDuration;
    }
}

public int getDuration() {
    return duration;
}
}

addNewSong() 方法的入口处,您创建了四个 Song 对象并将它们存储在局部变量中。退出该方法时局部变量会丢失,因此每次输入 addNewSong() 时都会有四首全新的歌曲。您可能需要将歌曲存储在 class 字段中并预先初始化它们(类似于您的 console 字段)。只需将这四行移出 addNewSong() 方法:

Song song1 = new Song();
Song song2 = new Song();
Song song3 = new Song();
Song song4 = new Song();

此外,我建议您考虑使用数组或 List 歌曲。当您添加新功能或增加歌曲数量时,您的代码很快就会变得一团糟。

每次调用 addNewSong() 方法时,您都在初始化 song1..4。您需要将初始化外部化,可能是构造函数。

顺便说一下,我会考虑使用 ArrayList<Song> 来实现更灵活的实施方式

首先你需要了解局部变量和作用域。其次,如果你的插槽是固定的,最好使用数组。

Song[] songs = new Songs[4];
for(int i=0;i<songs.length;i++){
   // Get input for song from user and use constructor
   songs[i] = new Song(name,artist,size,duration);
}

当你有构造函数时为什么需要单独设置属性?如果插槽不固定,您应该使用构造函数 itself.And ArrayList.

List<Song> songsList = new ArrayList<Song>();
// Get songs from user and add them to list.