Java 如何确保添加到数组中的对象ID不';如果不获取nullpointerexception,则无法存在?
我有一项任务,我要创建一个管理CD的程序。我使用的是switch语句,在第一种switch情况下,我必须向阵列中添加一张CD,但前提是阵列中不存在具有相同ID的CD 我试过了Java 如何确保添加到数组中的对象ID不';如果不获取nullpointerexception,则无法存在?,java,arrays,for-loop,if-statement,Java,Arrays,For Loop,If Statement,我有一项任务,我要创建一个管理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.pri
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-each循环,但没有任何效果。要么我得到空指针异常,要么cd不会添加到数组中。
我不允许有任何NullPointerException在代码中,我被困在这个问题上两天了,所以我希望有人在那里可以帮助我
对不起,我犯了错误。这是我第一次在Stacksoverflow上发帖
下面的代码是CD主类
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
是一个字符串
,因此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;
}
boolean-found=false;
对于(int i=0;i<计数器;++i){
if(cdArray[i].getCdId().equalsIgnoreCase(CDid)){
发现=真;
打破
}
}
如果(找到){
System.out.println(CDid+“已存在”);
}else if(计数器>=cdArray.length){
System.out.println(“数组已满”);
}否则{
cdArray[计数器]=新CD(CDid、CDGREEP、CD艺人、CDalbum、,
CDstorageSpot、CDtrackAmount、CDstorageAmount、CDprice);
++计数器;
}
另外请注意,除非您知道可以将其添加到阵列中,否则不需要创建
CD
。我认为问题在于您读取的方式不对。在处理字符串时,如果没有分隔符(在本例中为换行符),就无法将它们彼此分开。您应该使用Scanner.nextLine
,而不是Scanner.next
<代码>扫描仪。下一步一次只读取一个字符
现在,关于检查阵列中现有ID的方法:
//For循环检查是否有任何元素与CDId匹配。变量exists将被删除
//如果数组中已存在,则将其设置为true。
布尔存在=假;
如果(CDId!=null){
对于(int i=0;i
CDid.getCdId应该是CDid.getCdId(),我想。对不起,我是java新手,我不明白你的答案。我正在使用手动用户输入添加CD。我遇到的问题是CD无法添加内容。检查CD是否唯一的代码不起作用。如果我尝试为每个循环创建一个类似于:for(CD:cdArray){if(CDid.equalsIgnoreCase(CDid.getCdId()){cdArray[counter]=CD_obj;counter++;}的循环,我会得到一个空指针异常,我不允许使用try catch处理该异常,但也不允许使用CDid.equals(CDid.getCdId),似乎很奇怪。您正在询问对象是否等于其自己的Id。这些比较起来似乎很奇怪。此外,我找不到CD-类的equals-方法的实现,因此您可能想编写一个小布尔方法,给定一个CD Id字符串,在您的CD数组中循环,以查看它是否等于数组中任何对象的Id.
//For loop to check if any of the elements match the CDId. The variable exists will be
//set to true if it already exists in the array.
boolean exists = false;
if(CDId != null) {
for(int i = 0; i < cdArray.length; i++) {
if(cdArray[i] == null) continue;
if(CDId.equalsIgnoreCase(cdArray[i].getCdId())) {
exists = true;
break;
}
}
}
if(exists) {
//If the CDId already exists... (put your own code if you want, such as an error message)
} else {
//If the id doesn't exist in the array yet, add the cd_obj to the array...
boolean added = false;
for(int i = 0; i < cdArray.length; i++) {
if(cdArray[i] == null) {
cdArray[i] = cd_obj;
added = true;
break;
}
}
//if the cd_obj wasn't added to the array, notify the user.
if(!added) {
System.out.println("The array is full");
}
}