Hat
Hat

Reputation: 81

How to take input and save into an array and then print the array input?

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
    }
}

Upvotes: 2

Views: 441

Answers (1)

Assafs
Assafs

Reputation: 3275

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
    }
}

Upvotes: 1

Related Questions