2

I'm trying to make a 'bookshelf' that takes up to 25 books as input, prints out the newest added book and then if the user wants to print out the books, it'll print out the full array but I can't figure out how to do it :/ below is my code, I also have another class named Book that uses a toString method to attain book information and I'm trying to get my printBooks to use the same format as my toString method

package BookShelf;

import java.util.Scanner;
import java.util.Arrays;
  /**
   * this class adds a book object and adds them to a book shelf
   * @author H
   * @verison 9/30/2017
   */
  public class BookShelf{//class
  private Scanner scan;
  private String answer;
  public String line1;
  public String line2;
  public String line3;
  public String line4;
  public String line5;
  String[] book = new String[25];
  //book = new String[25];
  //private Arrays intName;
  private Boolean b;

/**
*constructor
*/
BookShelf(){ //constructor
  this.scan = new Scanner(System.in);
  line1 = " ";
  line2 = " ";
  line3 = " ";
  line4 = " ";
  line5 = " ";
  //this.book = new book;
}

/**
 * method that adds a book to the array
 * @return String, has new added book info in it
 */

public void addBook(){ //method
  //do{
  this.scan = new Scanner(System.in);
  System.out.println("Please enter the author's last name.");
  String line1 = scan.nextLine();
  System.out.println("Please enter the author's first name.");
  String line2 = scan.nextLine();
  System.out.println("Please enter the book title.");
  String line3 = scan.nextLine();
  System.out.println("Please enter the publisher.");
  String line4 = scan.nextLine();
  System.out.println("Please enter the publication year.");
  String line5 = scan.nextLine();
  System.out.println("Added book: " + line1 + ", " + line2 + " (" + line5 + ")" + ". " + line3 + ". " + line4);
  //} while ( b == false);{
      //for (int i = 0; i < book.length; i++){
        //if(book[i] == null){
          //this.b = true;
        //}
      //}
  //}
 //maybe make a counter to see if arrary is full and then do an if statement thats arr !full then ask questions

}


// Book[]library = newBook[n] //array


public void printBooks(){
  for( int i = 0; i < book.length; i++){
    System.out.println(book[i]);
  }
}

 //public void addedBookInfo(){
   //System.out.println(Arrays.toString(intName));
   //^this should work once arrays are figured out
//}


/**
* method that asks user for a command and then follows the command
* follows the command by calling other methods
*/
public void interact(){
  System.out.println("Would you like to add, print, or quit?");
  this.answer = scan.nextLine();
  if (answer.equals("add") || answer.equals("Add")){
    addBook();
  }
  else if (answer.equals("print") || answer.equals("Print")){
    printBooks();
  }
  else{
  }
}

/**
 * main method
 * @param rank valid values: args
 */
    public static void main(String[] args){ 
    BookShelf bs = new BookShelf();  //makes new shelf
    bs.interact(); //interacts with shelf
    }
}
3
  • unrelated, but package should be named better Commented Oct 1, 2017 at 18:25
  • use collections Commented Oct 1, 2017 at 18:32
  • 1
    On a totally unrelated note, I'd suggest reducing answer.equals("add") || answer.equals("Add") to answer.equalsIgnoreCase("add") Commented Oct 1, 2017 at 19:25

1 Answer 1

1

You have a few things in your implementation that seem a bit cumbersome, but I think this code might do the trick for your requirements:

package BookShelf;

import java.util.Scanner;
import java.util.Arrays;
  /**
   * this class adds a book object and adds them to a book shelf
   * @author H
   * @verison 9/30/2017
   */
  public class BookShelf {
  Scanner scan;
  static final int MAX_SIZE = 25;
  int index;
  String[] books;

  public BookShelf() {
      //In the constructor, initialize the array and the index.
      //it's best practice not to initialize resources like scanner 
      //you may forget to close them.
      books = new String[MAX_SIZE];
      index = 0;
  }
/**
 * method that adds a book to the array
 * @return String, has new added book info in it
 */

public void addBook() {
  //Check you have room for the book you want to add.
  if (index == MAX_SIZE) {
      System.out.println("There are "+MAX_SIZE+" in the book shelf");
  } else {
      this.scan = new Scanner(System.in);
      System.out.println("Please enter the author's last name.");
      String line1 = scan.nextLine();
      System.out.println("Please enter the author's first name.");
      String line2 = scan.nextLine();
      System.out.println("Please enter the book title.");
      String line3 = scan.nextLine();
      System.out.println("Please enter the publisher.");
      String line4 = scan.nextLine();
      System.out.println("Please enter the publication year.");
      String line5 = scan.nextLine();
      String book =  line1 + ", " + line2 + " (" + line5 + ")" + ". " + line3 + ". " + line4;
      System.out.println("Added book: " + book);
      books[index] = book;
      index++;

      //Close the scanner
      scan.close();
  }
}  

public void printBooks() {
  for( int i = 0; i < books.length; i++){
    System.out.println(books[i]);
  }
}

/**
* method that asks user for a command and then follows the command
* follows the command by calling other methods
*/
public void interact(){
  Scanner scan = new Scanner(System.in);
  String answer = scan.nextLine();
  while (!answer.equalsIgnoreCase("quit")) {
    System.out.println("Would you like to add, print, or quit?");
    answer = scan.nextLine();
    if (answer.equalsIgnoreCase("add")) {
      addBook();
    }
    else if (answer.equalsIgnoreCase("print")) {
      printBooks();
    }
  }
  scan.close();
}

/**
 * main method
 * @param rank valid values: args
 */
    public static void main(String[] args){ 
      BookShelf bs = new BookShelf();  //makes new shelf
      bs.interact(); //interacts with shelf
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for the advice but I just changed my code and now I have a nullPointerException and don't know how to fix it
@Hat I'll be happy to help you. Did you change the code to what I wrote in the answer? Where do you get the exception?
I changed it and now I'm not getting the nullPointerException but the print part doesn't work so I feel like I'm at the beginning of my problem
what's good is that when I call print it gives me 25 nulls so at least the count works
@Hat, I keep finding small changes I can do to improve the code. I just added another change to the example. If the last version wasn't fully functional, I will have to try again tomorrow.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.