gpt4 book ai didi

java - 代码中的扫描仪错误,使用引用变量构建数组

转载 作者:行者123 更新时间:2023-12-01 21:56:44 26 4
gpt4 key购买 nike

我对编码还很陌生,所以如果有明显的修复,我深表歉意。

我正在尝试构建一个可以存储书籍对象的库,但在我可以使用功能之前,我需要整理一个数组。 Java 本身在我的侧栏中没有显示任何错误,但是当通过控制台运行时,会出现错误。

下面我发布了我的 Library.java 类。

import java.util.Scanner;


public class Library {

public static void main(String[] args){

Scanner input = new Scanner(System.in);

Book[] books = new Book[2];
String Title;
String Author;
String BookID;
Boolean onLoan;
int NumberofLoans;

for( int i = 0; i < books.length; i++){
System.out.print("Enter the title of the book: ");
Title = input.next();
books[i] = new Book();
books[i].setTitle(Title);
System.out.println("");

System.out.print("Enter the Author of the book: ");
Author = input.next();
books[i].setAuthor(Author);
System.out.println("");

System.out.print("Enter the Book's ID: ");
BookID = input.next();
books[i].setBookID(BookID);
System.out.println("");

NumberofLoans = 0;
books[i].setNumberofLoans(NumberofLoans);

onLoan = false;
books[i].setonLoan(onLoan);

input.close();
}

for(int i = 0; i < books.length; i++){
System.out.println("Title: " + books[i].getTitle());
System.out.println("Author: " + books[i].getAuthor());
System.out.println("BookID: " + books[i].getBookID());
System.out.println("Times loaned: " + books[i].getNumberofLoans());
System.out.println("In library: " + books[i].getonLoan());

}
}
}

下面列出了控制台错误本身:

Exception in thread "main" Enter the title of the book:
java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Scanner.java:1070)
at java.util.Scanner.next(Scanner.java:1358)
at Library.main(Library.java:19)

有人可以指出我的代码中的错误来自何处吗?那么,我可以进行更改来阻止此错误的发生吗?

为了确保列出所有内容,我将发布 Book.java,以防错误源自那里。

public class Book {

private String Title;
private String Author;
private String BookID;
private Boolean onLoan;
private int NumberofLoans;



public Book(){
Title = new String();
Author = new String();
BookID = new String();
onLoan = false;
NumberofLoans = 0;

}

public void setTitle(String title) {
this.Title = title;
}

public void setAuthor(String author) {
this.Author = author;
}

public void setBookID(String bookID) {
this.BookID = bookID;
}

public void setonLoan(Boolean onLoan) {
this.onLoan = onLoan;
}

public void setNumberofLoans(int numberofLoans) {
this.NumberofLoans = numberofLoans;
}

public String getTitle() {
return Title;
}

public String getAuthor() {
return Author;
}

public String getBookID() {
return BookID;
}

public Boolean getonLoan() {
return onLoan;
}

public int getNumberofLoans() {
return NumberofLoans;
}}

最佳答案

Library类的第一个循环中导致了这种情况。具体来说,这一行:

input.close();

因为您在整个程序完全使用扫描仪之前就将其关闭。 Scanner 在循环第一次运行时可用。但第一次执行后,您关闭扫描仪。因此,对于连续的循环运行,扫描仪关闭,因此出现错误。

循环结束后添加此行。换句话说,像这样:

for( int i = 0; i < books.length; i++){
// ... Whatever
}

input.close();

关于java - 代码中的扫描仪错误,使用引用变量构建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34179847/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com