2016-01-18 79 views
-3

我無法將鍵入的信息寫入文本文件並將其存儲在數組中。我在文本文件中獲取空指針異常嘗試將新的圖書對象打印到該列表中。我必須打開文本文件,寫入它,然後將新對象添加到數組中。當您按下「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(); 
    } 

} 
+1

可能重複[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how -DO-I-FIX-它) – OldProgrammer

回答

0

而不是使用鍵盤,爲什麼不使用掃描儀?只需使用一箇中間String對象來保存用戶輸入的內容。這可以幫助一點菜單。

而且通過這一點,那麼你可以複製哪些用戶輸入到書直接

2

構造應分配的參數值的字段。爲了區分字段名稱,可以使用this

public Book(String title, String author, int price) { 
    this.title = title; 
    this.author = author; 
    this.price = price; 
} 

參數的名字只被當地人稱爲並且不要讓「有線」的領域(如在一些比較少見的編程語言)。