我無法將鍵入的信息寫入文本文件並將其存儲在數組中。我在文本文件中獲取空指針異常嘗試將新的圖書對象打印到該列表中。我必須打開文本文件,寫入它,然後將新對象添加到數組中。當您按下「1」菜單時,我也遇到格式化菜單問題,菜單要求您輸入標題和作者,同時難以爲每一個輸入單獨的答案。這是我的代碼:需要幫助寫入文本文件並將數據存儲在數組中
Book類:承載用於某些變量
/**
* 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;
}
}
簿輸入一個toString方法以及信息:
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();
}
}
可能重複[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how -DO-I-FIX-它) – OldProgrammer