Java Objects Array: How to print only first elements of objects?

Mr. Engineer :

I'm trying to create a program that takes book names, number of pages, and publishing year as user input. When user does not input anything to name field, program should ask user what will be printed: only names of the books or all information given by user. Here is my main code:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int index = 0;
        Scanner reader = new Scanner(System.in);
        ArrayList<Books> book = new ArrayList<>();
        while (true) {
            System.out.println("Name of the book: ");
            String name = reader.nextLine();
            if (name.isEmpty()) {
                break;
            }
            System.out.println("Number of pages: ");
            int pages = Integer.parseInt(reader.nextLine());
            System.out.println("Year: ");
            int year = Integer.parseInt(reader.nextLine());
            book.add(new Books(name,pages,year));
        }
        while (true) {
            System.out.println("What do you want to print?");
            String whatwillbeprinted = reader.nextLine();
            if (whatwillbeprinted.equals("everything")) {
                while(index < book.size()) {
                    System.out.println(book.get(index));
                    index++;
                }
            }
            if (whatwillbeprinted.equals("names")) {
                while(index < book.size()) {
                    // print only names of the books
                }
            }
        }
        }
    }

And here is my Java class named Books:

public class Books {
    private String name;
    private int pages;
    private int year;

    public Books(String name, int pages, int year) {
        this.name = name;
        this.pages = pages;
        this.year = year;
    }
    @Override
    public String toString() {
        return this.name + ", " + this.pages + ", " + this.year;
    }
}

Everything operates correctly until that last while-statement. How can I print only first elements of my objects (names of the books)? Thanks in advance.

Destroyer :
  public class Books {
    private String name;
    private int pages;
    private int year;

    public Books(String name, int pages, int year) {
        this.name = name;
        this.pages = pages;`
        this.year = year;
    }
    @Override
    public String toString() {
        return this.name + ", " + this.pages + ", " + this.year;
    }

    public String getName(){
        return name;
    }
 }

Then calling book.getName () you can get the name of the book, now just write a loop in which display the name of each book

P.S confusion about the name of the class: The name Books is incorrect since the class stores information about only one book. Book - correct title

Book Title Code:

for(int i = 0;i<book.lenght;i++)
{
   System.out.println("Name of book: " + book[i].getName());
} 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=344563&siteId=1