我有一项任务,要创建一个管理 CD 的程序。我正在使用 switch 语句,在第一个 switch 情况下,我必须将 CD 添加到阵列中,但前提是阵列中尚不存在具有相同 ID 的 CD。
我尝试过
if (CDid != null && !CDid.equals(CDid.getCdId) {
cdArray[counter] = cd_obj;
counter++;
} else {
counter = cdArray.length-1;
System.out.println("The array is full");
}
我还在 if 语句之前尝试了 for every 循环,但没有任何效果。我要么得到空指针异常,要么 cd 不会添加到数组中。 我不允许在代码中出现任何空指针异常,而且我已经被这个问题困扰了 2 天,所以我希望有人可以帮助我!
对我犯的任何错误表示歉意,这是我第一次在 Stacksoverflow 上发布问题
以下代码是CD Main类
public class Cd_main {
public static void main(String[] args){
boolean running = true;
String CDid, CDgenre, CDartist, CDalbum, CDstorageSpot, CDtrackAmount,
CDstorageAmount CDprice, searchGenre, deleteCD ;
CD[] cdArray = new CD[25];
int counter = 0;
int choice;
Scanner scanner = new Scanner(System.in);
while(running){
............................
............................
choice = scanner.nextInt();
switch(choice){
case 1:
System.out.println("......");
System.out.println("............");
CDid = scanner.next();
CDgenre = scanner.next();
CDartist = scanner.next();
CDalbum = scanner.next();
CDstorageSpot= scanner.next();
CDtrackAmount= scanner.next();
CDstorageAmount = scanner.next();
CDprice = scanner.next();
CD cd_obj = new CD(CDid, CDgenre, CDartist, CDalbum,
CDstorageSpot, CDtrackAmount, CDstorageAmount, CDprice);
if(CDid.equalsIgnoreCase(CDid.getCdId())){
cdArray[counter] = cd_obj;
counter++;
}else{
counter = cdArray.length-1;
System.out.println("The array is full");
}
}
break;
```
The CD class:
```
public class CD {
public String cd_ID;
public String genre;
public String artist;
public String album;
public String storageSpot;
public String trackAmount;
public String storageAmount;
public String price;
public CD() {
}
public CD(String cd_ID, String genre, String artist, String album,
String storageSpot, String trackAmount, String storageAmount,
String price){
this.cd_ID = cd_ID;
this.genre = genre;
this.artist = artist;
this.album = album;
this.storageSpot = storageSpot;
this.trackAmount = trackAmount;
this.storageAmount = storageAmount;
this.price = price;
}
public String getCdId() {
return this.cd_ID;
}
public void setCdId(String cd_ID) {
this.cd_ID = cd_ID;
}
public String getGenre() {
return this.genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public String getArtist() {
return this.artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getAlbum() {
return this.album;
}
public void setAlbum(String album) {
this.album = album;
}
public String getStorageSpot() {
return this.storageSpot;
}
public void setStorageSpot(String storageSpot) {
this.storageSpot = storageSpot;
}
public String getTrackAmount() {
return this.trackAmount;
}
public void setTrackAmount(String trackAmount) {
this.trackAmount = trackAmount;
}
public String getStorageAmount() {
return this.storageAmount;
}
public void setStorageAmount(String storageAmount) {
this.storageAmount= storageAmount;
}
public String getPrice() {
return this.price;
}
public void setPrice(String price){
this.price = price;
}
}
最佳答案
CDid
是一个 String
,因此 CDid.getCdId()
根本没有任何意义。您需要遍历整个数组以确保 id 不会退出。
例如:
boolean found = false;
for (int i = 0; i < counter; ++i) {
if (cdArray[i].getCdId().equalsIgnoreCase(CDid)) {
found = true;
break;
}
}
if (found) {
System.out.println(CDid + " already exists");
} else if (counter >= cdArray.length) {
System.out.println("The array is full");
} else {
cdArray[counter] = new CD(CDid, CDgenre, CDartist, CDalbum,
CDstorageSpot, CDtrackAmount, CDstorageAmount, CDprice);
++counter;
}
另请注意,您不需要创建 CD
,除非您知道能够将其添加到数组中。
关于java - 如何确保添加到数组的对象的 ID 不存在而不出现 nullpointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57269442/