问题描述
下面的是我的代码中的一个类,主要问题就是在我添加DVD过后,再删除添加的DVD.然后添加新的DVD后就会报错报错的是这句话:if(dvd[i].name.compareToIgnoreCase("")==0){packagecom.jb.cdjj.s1t112.chapter16.t1;importjava.util.Scanner;importjavax.swing.JOptionPane;publicclassMiniDvdManager{Scannerinput=newScanner(System.in);MiniDvd[]dvd=newMiniDvd[100];intindex=0;inttemp=-1;/***do{inttemp=-1;temp=JOptionPane.showConfirmDialog(null,*"<html><fontcolor='red'>红色的字</font><br>*其他字</html>","chooseone",JOptionPane.YES_NO_OPTION);}while(temp==0);*//**建立主菜单,菜单选项有:1.新增,2.查看,3删除,4.借出,5.归还,6.退出*/publicvoidmenuDvd(){initDvd();booleanisRight=true;do{Stringnum=JOptionPane.showInputDialog("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^n"+"┏┯┓┏┯┓D┏┯┓┏┯┓┏┯┓n"+"┠迷┨┠你┨v┇┠管┨┠理┨┠器┨n"+"┗┷┛┗┷┛D┗┷┛┗┷┛┗┷┛n"+"-------------------------------------------------------------n"+"┏━━━━━━━━━┓n"+"┃◤1.新增DVD◥┃n"+"┃┇2.查看DVDゞ┃n"+"┃┇3.删除DVDゞ┃n"+"┃┇4.借出DVDゞ┃n"+"┃┇5.归还DVDゞ┃n"+"┃◣6.退出DVD◢┃n"+"┗━━━━━━━━━┛n"+"ˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇn"+"请选择(数字1-6):");intno=Integer.parseInt(num);switch(no){case1:addDvd();break;case2:lookUpDvd();break;case3:deleteDvd();break;case4:lendDvd();break;case5:chargeDvd();break;case6:JOptionPane.showMessageDialog(null,"谢谢使用!");System.exit(0);default:JOptionPane.showMessageDialog(null,"<html><fontcolor='red'>输入错误,请重新输入!");continue;}}while(isRight);}/**初始化*/publicvoidinitDvd(){for(inti=0;i<dvd.length;i++){dvd[i]=newMiniDvd();dvd[i].name="";dvd[i].state=-1;dvd[i].date=null;}}/**新增DVD*/publicvoidaddDvd(){do{Stringname=JOptionPane.showInputDialog("···→新增DVDnn请输入名称:");for(inti=0;i<dvd.length;i++){if(dvd[i].name.compareToIgnoreCase("")==0){dvd[i].name=name;dvd[i].state=1;JOptionPane.showMessageDialog(null,"<html><fontcolor='green'>添加《"+dvd[i].name+"》DVD成功!");break;}}//JOptionPane.showConfirmDialog(null,//"<html><fontcolor='red'>红色的字</font><br>其他字</html>","chooseone",//JOptionPane.YES_NO_OPTION);temp=JOptionPane.showConfirmDialog(null,"<html><fontcolor='red'>是否继续添加DVD","chooseone",JOptionPane.YES_NO_OPTION);}while(temp==0);}/**查看DVD*/publicvoidlookUpDvd(){StringBuffersearDvd=newStringBuffer();inttemp=0;for(inti=0;i<dvd.length;i++){if(dvd[i].name.compareToIgnoreCase("")!=0){searDvd.append(dvd[i].toString()+"n");temp++;continue;}elseif(temp==0&&i==dvd.length-1){JOptionPane.showMessageDialog(null,"<html><fontcolor=#CE0000>尚未添加任何DVD信息,自动返回主菜单");return;}}JOptionPane.showMessageDialog(null,searDvd);}/**删除DVD*/publicvoiddeleteDvd(){StringdelName=JOptionPane.showInputDialog("···→删除DVDnn请输入名称:");Stringname=delName;for(inti=0;i<dvd.length;i++){if(dvd[i].name.compareToIgnoreCase("")!=0&&dvd[i].name.equalsIgnoreCase(name)&&dvd[i].state==1){intj=i;while(dvd[j+1].name.compareToIgnoreCase("")!=0){dvd[j].name=dvd[j+1].name;dvd[j].state=dvd[j+1].state;dvd[j].date=dvd[j+1].date;j++;}//最后一个不为空的元素置空dvd[j].name=null;dvd[j].date=null;JOptionPane.showMessageDialog(null,"删除《"+name+"》成功!");return;}elseif(dvd[i].name!=null&&dvd[i].name.equalsIgnoreCase(name)&&dvd[i].state==0){break;//标识位}elseif(dvd[i].name.equalsIgnoreCase("")){JOptionPane.showMessageDialog(null,"<html><fontcolor=#CE0000>未找到《"+name+"》DVD信息,自动返回主菜单!");return;}}}/**借出DVD*/publicvoidlendDvd(){while(true){StringdelName=JOptionPane.showInputDialog("···→借出DVDnn请输入名称:");Stringname=delName;for(inti=0;i<dvd.length;i++){if(dvd[i].name==null){//无匹配JOptionPane.showMessageDialog(null,"没有找到匹配信息!");break;}elseif(dvd[i].name.equals(name)&&dvd[i].state==1){JOptionPane.showConfirmDialog(null,"找到匹配可借!是否借出?");return;}elseif(dvd[i].equals(name)&&dvd[i].state==0){JOptionPane.showConfirmDialog(null,"找到匹配,已被借出!");break;}}}}/**归还DVD*/publicvoidchargeDvd(){}}