需要帮助写入文本文件并将信息存储在数组中

时间:2016-01-18 19:52:43

标签: java arrays text

我无法将键盘输入的信息写入文本文件并将其存储在数组中。当我尝试将新书对象打印到该列表时,我在文本文件中得到一个空指针异常。我必须打开文本文件,写入它,并将新对象添加到数组中。我也在菜单上遇到格式化问题,当你按“1”时,菜单会询问你的标题和作者同时难以为每个人输入一个单独的答案。这是我的代码:

Book Class:带有toString方法以及某些变量的信息

/**
 * 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();
    }

}

2 个答案:

答案 0 :(得分:2)

构造函数应将参数值分配给字段。要对字段名称进行歧义处理,可以使用this

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

参数名称仅在本地知道,并且不会“连接”到字段(如在一些更罕见的编程语言中)。

答案 1 :(得分:0)

为什么不使用扫描仪,而不是使用键盘?只需使用中间String对象来保存用户输入的内容。这可能对菜单有所帮助。

同样通过使用它,您可以直接复制用户输入书中的内容