如何获取输入并保存到数组中然后打印数组输入?

时间:2017-10-01 18:14:10

标签: java arrays

我正在尝试制作一个多达25本书作为输入的“书架”,打印出最新添加的书籍,然后如果用户想要打印出书籍,它将打印出整个阵列但是我无法弄清楚如何做到:/下面是我的代码,我还有另一个名为Book的类,它使用toString方法获取书籍信息,我试图让我的printBooks使用与toString方法相同的格式

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

1 个答案:

答案 0 :(得分:1)

您的实现中有一些看起来有点麻烦,但我认为这段代码可能会满足您的要求:

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