I am having trouble writing the information typed in from the keyboard to a text file and storing it in an array.I get a null pointer exception within the text file when I try to print a new book object to that list. I have to open the text file, write to it, and add the new object to an array. I also am having formatting troubles with the menu, when you press "1" the menu asks you for the title and author simultaneously making it difficult to type an individual answer for each one. Here is my code:
Book Class: carries a toString method as well as information for certain variables
/**
* 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;
}
}
Book Input:
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;
}
}
Book Output:
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();
}
}
Main Driver:
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();
}
}