1

I'm a beginner in java and I was wondering if there was a way to change the name of an object in the main class with input? For example I got this code:

while(!answer.equals("stop"))
    {
    //Enters book's information  and stores it in the object book1  
    System.out.println("Book-" + count);    
    title = input.next();
    author = input.next();
    ISBN = input.next();
    copies = input.nextInt();
    Book book1 = new Book(title,author, ISBN, copies);
    printLn("");
    printLn("Do you wish stop adding books? N || stop");      
    answer = input.next();
    }

I want to keep adding new books until I write stop when prompted but of course without changing the name it will keep adding the data to the same object. Is it possible or do I need to keep making new book objects with: Book etc = new Book(title,author,ISBN, copies)

"Correction to my code" Like Kevin mentioned, an array is the main idea to store these values but it can be full due to its static value but I could use an expandcapacity method for when n--books are entered and the array is full it expands the array in x size. Thank you!

2
  • 1
    do you know the concepts of list, array, set, collection? Commented Apr 20, 2013 at 22:25
  • 1
    You will need lot of Book variables in order to make this work. So, in order to not declare lot of unknown variables by hand you can use an array or a collection like a List<Book> backed up by ArrayList<Book>. Commented Apr 20, 2013 at 22:25

1 Answer 1

7

The code should store each book in a List so that they may be accessed later in the code. The name is really of no importance other than it identifies your object within code. Even if you could change the name of the local variable book your issue would still remain.

The issue you experience is more related to scope and object instances. When you call new Book(..) you create a new instance of a book. This instance of book's scope is limited to the code block {} executed by the while loop. This means that outside the loop the instance of book is inaccessible.

In order to access the instance of book outside the loop, you could create a book outside the loop like so:

Book book;

while(...){
   book = new Book(...);
}

The issue with this method is that you are creating several instances of book, so the reference to the book will get overwritten with the newest book for each iteration of the loop.

This creates the necessity for something to hold multiple books. Instantly an array may come to mind, however the size of arrays is static and a user may enter 1..n books. This does not make an array a good choice for storing the books.

This is where the List and ArrayList come into play. A List is a data structure that holds multiple object instances. It can be expanded easily using the add(Object) method. A full description of a List and an ArrayList is beyond the scope of this answer, but I offer the following resource: http://docs.oracle.com/javase/tutorial/collections/

Final Solution

    List<Book> books = new ArrayList<Book>();
    while(!answer.equals("stop"))
        {
        //Enters book's information  and stores it in the object book1  
        System.out.println("Book-" + count);    

        title = input.next();
        author = input.next();
        ISBN = input.next();
        copies = input.nextInt();

        Book book1 = new Book(title,author, ISBN, copies);
        books.add(book1);

        printLn("");
        printLn("Do you wish stop adding books? N || stop");      
        answer = input.next();
     }

     //Iterating the book list outside the loop
     for(Book book:books){
       //this call may vary depending on the book implementation
       System.out.println(book.getTitle());
     }
Sign up to request clarification or add additional context in comments.

4 Comments

While you have a point in this answer, remember this words in OP: I'm a beginner in java. Based on this, looks like OP doesn't even know how an ArrayList works behind the scenes, so the first step would be teaching arrays concepts and providing an array solution.
@LuiggiMendoza I tried to elaborate without giving a course in Collections 101
@KevinBowersox Thank you Kevin for you patience. That really helped.
@JorgeAVegaVigo No problem! Glad I could help. Remember we all started in your spot at one point and its not where you start, but where you progress to that matters.

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.