Java 需要帮助写入文本文件并在数组中存储信息吗
我无法将从键盘键入的信息写入文本文件并将其存储在数组中。当我尝试将新书对象打印到该列表时,文本文件中出现空指针异常。我必须打开文本文件,写入它,然后将新对象添加到数组中。我的菜单也有格式问题,当你按1时,菜单会同时要求你输入标题和作者,这样就很难为每一个输入单独的答案。这是我的密码: Book类:包含toString方法以及特定变量的信息Java 需要帮助写入文本文件并在数组中存储信息吗,java,arrays,text,Java,Arrays,Text,我无法将从键盘键入的信息写入文本文件并将其存储在数组中。当我尝试将新书对象打印到该列表时,文本文件中出现空指针异常。我必须打开文本文件,写入它,然后将新对象添加到数组中。我的菜单也有格式问题,当你按1时,菜单会同时要求你输入标题和作者,这样就很难为每一个输入单独的答案。这是我的密码: Book类:包含toString方法以及特定变量的信息 /** * This class holds information regarding the title, author, and price vari
/**
* This class holds information regarding the title, author, and price variables
* @author
*
*/
public class Book {
String title;
String author;
double price;
public Book(String title, String author, int price) {
}
public String toString(){
return title + " by" + author + " ," + price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
书籍输入:
public class BookInput {
Scanner keyboard= new Scanner(System.in);
/**
* Reads the menu choice from the user
* @return
*/
public int readMenuChoice(){
int choice=keyboard.nextInt();
return choice;
}
/**
* Reads information for a new book object
* @return
*/
public Book readBook(){
System.out.print("Enter the book title: ");
String title=keyboard.nextLine();
System.out.print("Enter the author: ");
String author=keyboard.nextLine();
System.out.print("Enter the price: ");
int price=keyboard.nextInt();
Book b=new Book(title,author,price);
return b;
}
/**
* Reads the entire book list and returns the amount of objects in the array
* @param bookArray
* @param filename
* @return
*/
public int readBookList(Book[] bookArray, String filename){
Scanner inputStream=null;
int counter=0;
try
{
inputStream=new Scanner(new File(filename));
}
catch(FileNotFoundException e){
System.out.println("Error opening the file.");
System.exit(0);
}
for(int i=0; i<filename.length();i++){
bookArray[i]=readBook();
counter++;
}
return counter;
}
}
为什么不用扫描仪而不用键盘呢?只需使用中间字符串对象来保存用户输入的内容。这可能对菜单有点帮助
同样,通过使用它,您可以直接复制用户在书中输入的内容构造函数应该将参数值分配给字段。要对字段名进行二义化,可以使用以下命令 参数名仅在本地已知,不会像在一些更为罕见的编程语言中那样连接到字段。可能重复
public class BookOutput {
PrintWriter outputStream=null;
/**
* Prints the menu for the user
*/
public void printMenu(){
System.out.println("1.Add a new book");
System.out.println("2.Display the book list");
System.out.println("3.Quit");
}
/**
* Prints the list of books that have been entered by the user
* @param bookArray
* @param size
*/
public void printBookList(Book[] bookArray, int size){
for(int i=0;i<size; i++){
System.out.println(bookArray[i].toString());
}
}
/**
* Opens the file to be written on
* @param filename
*/
public void openFileForAppend(String filename){
try
{
outputStream= new PrintWriter(filename);
}
catch(FileNotFoundException e){
System.out.println("Error opening the file.");
System.exit(0);
}
}
/**
* Writes information regarding a new book object to the file
* @param book
*/
public void writeBookToFile(Book book){
outputStream.println(book.toString());
}
/**
* closes the file
*/
public void closeFile(){
outputStream.close();
}
}
public static void main(String[] args) {
BookInput i= new BookInput();
BookOutput o= new BookOutput();
Book [] bookArray = new Book[20];
String filename= "BookList.txt";
int size=i.readBookList(bookArray, filename);
o.openFileForAppend(filename);
o.printMenu();
int choice=i.readMenuChoice();
while(choice!=3){
switch(choice){
case 1:
Book book=i.readBook();
size++;
o.writeBookToFile(book);
break;
case 2:
o.printBookList(bookArray, size);
break;
default:
System.out.println("Invalid menu choice. Please try again");
break;
}
o.printMenu();
choice=i.readMenuChoice();
}
o.closeFile();
}
}
public Book(String title, String author, int price) {
this.title = title;
this.author = author;
this.price = price;
}