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!
Bookvariables in order to make this work. So, in order to not declare lot of unknown variables by hand you can use anarrayor a collection like aList<Book>backed up byArrayList<Book>.