Java 在我的迷你库程序中,如何在没有NullPointerException的情况下访问else语句?
我创建了一个小程序,其中有一个100个字符串的数组(null,除非使用另一个函数添加值) 如果我试图发出一本不在数组中的书,它会给我NullPointerException错误,而不是不运行If语句并打印找不到的书Java 在我的迷你库程序中,如何在没有NullPointerException的情况下访问else语句?,java,arrays,oop,if-statement,nullpointerexception,Java,Arrays,Oop,If Statement,Nullpointerexception,我创建了一个小程序,其中有一个100个字符串的数组(null,除非使用另一个函数添加值) 如果我试图发出一本不在数组中的书,它会给我NullPointerException错误,而不是不运行If语句并打印找不到的书 public void issueBook() { Scanner sc = new Scanner(System.in); System.out.print("Enter book title to issue: "); String B
public void issueBook() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter book title to issue: ");
String BookTitle = sc.nextLine(); //Asks for book name to be compared in the array
for (int i = 0; i < Books.length; i++) { //loop to check
if (this.Books[i].equals(BookTitle)) { //if statement to check the condition
System.out.println("Book has been issued.");
this.Books[i] = null; //If the book has been issued, it will null it's position in the array
return;
}
}
System.out.println("ERROR: Book not found."); //If the book isn't found, it should print this
}
public void发行本(){
扫描仪sc=新的扫描仪(System.in);
System.out.print(“输入要发行的图书标题:”);
字符串BookTitle=sc.nextLine();//要求在数组中比较图书名称
对于(int i=0;i
如果this.Books
至少在开始时填充了null
,那么在调用其任何方法之前,您需要检查this.book[i]
是否不为null,否则您将得到那些NullPointerException
:
for(int i=0;i
我建议你用一个新的。通过这种方式,您可以在创建图书时简单地添加图书,并且在从
This.books
变量中提取图书时,您永远不需要处理空点异常,因为只有完全创建的图书才会被添加到列表中。如果This.books
至少在最初填充了null
,然后,在调用其任何方法之前,您需要检查this.book[i]
是否不为null,否则您将得到那些NullPointerException
:
public void issueBook(String[] Books) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter book title to issue: ");
String BookTitle = sc.nextLine(); //Asks for book name to be compared in the array
for (int i = 0; i < Books.length; i++) { //loop to check
if (this.Books[i]!= null && this.Books[i].equals(BookTitle)) { //if statement to check the condition
System.out.println("Book has been issued.");
this.Books[i] = null; //If the book has been issued, it will null it's position in the array
return;
}
}
System.out.println("ERROR: Book not found."); //If the book isn't found, it should print this
}
for(int i=0;i
我建议你用一个新的。通过这种方式,您可以在创建图书时简单地添加图书,并且从This.books
变量中提取图书时,您永远不需要处理NullPointerException
,因为只有完全创建的图书才会附加到列表中。public void issueBook(String[]books){
public void issueBook(String[] Books) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter book title to issue: ");
String BookTitle = sc.nextLine(); //Asks for book name to be compared in the array
for (int i = 0; i < Books.length; i++) { //loop to check
if (this.Books[i]!= null && this.Books[i].equals(BookTitle)) { //if statement to check the condition
System.out.println("Book has been issued.");
this.Books[i] = null; //If the book has been issued, it will null it's position in the array
return;
}
}
System.out.println("ERROR: Book not found."); //If the book isn't found, it should print this
}
扫描仪sc=新的扫描仪(System.in);
System.out.print(“输入要发行的图书标题:”);
字符串BookTitle=sc.nextLine();//要求在数组中比较图书名称
对于(int i=0;i
公共作废发行电子书(字符串[]书籍){
扫描仪sc=新的扫描仪(System.in);
System.out.print(“输入要发行的图书标题:”);
字符串BookTitle=sc.nextLine();//要求在数组中比较图书名称
对于(int i=0;i
您只需将this.Books[i].equals(BookTitle)
更改为BookTitle.equals(this.Books[i])
您只需将this.Books[i].equals(BookTitle)
更改为BookTitle.equals(this.Books[i])