0

I have an arraylist and my goal is to keep adding to an object to the arraylist until the user types in a value. I have two different classes. I have tried to use scanner but that only changes the name of the variable and doesn't create a new object. Would I have to inherit from the other class. How would I keep increasing the size of the arraylist? Any help is appreciated.

Below is my first class:

public class Real_Entertainment 
{
    String BookName; 
    String Author; 
    double Rating; 
    int Purchases;

    public Real_Entertainment(String BookName, String Author, double Rating, int Purchases, int i)
    {
        this.BookName = BookName;
        this.Author = Author;
        this.Rating = Rating;
        this.Purchases = Purchases;
    }
    public void setBookName(String BookName)
    {
        this.BookName = BookName;
    }
    public void setAuthor (String Author )
    {
        this.Author = Author;
    }
    public void setRating (double Rating)
    {
        this.Rating = Rating;
    }
    public void setPurchased (int Purchases)
    {
        this.Purchases = Purchases;
    }

}

Below is my main class:

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

public class Real_Main 
{
    public static void main(String[] args) 
    {
        ArrayList<Real_Entertainment> print = new ArrayList<Real_Entertainment>();
        Real_Entertainment Book1 = new Real_Entertainment("The Nickel Boys ", "Colson 
        Whitehead", 4.3, 500,000);
        Real_Entertainment Book2 = new Real_Entertainment("Olive, Again", "Elizabeth Strout", 6, 
        321,000);
        Real_Entertainment Book3 = new Real_Entertainment("Gingerbread", "Helen Oyeyemi", 2, 
        681,000);
        Real_Entertainment Book4 = new Real_Entertainment ("On Earth We're Briefly Gorgeous", 
        "Ocean Vuong", 2, 421,000);

        print.add(Book1);
        print.add(Book2);
        print.add(Book3);
        print.add(Book4);
    }

    public void addToList(ArrayList<Real_Entertainment> print)
    { 

         //The method where it adds to the arraylist    
    }

}
5
  • The problem is unclear, you are already adding to a list using print.add . Please show the attempt you made with scanner. Commented Feb 28, 2020 at 4:20
  • Thanks for the response. I have already added 4 objects without the scanner. Here is my failed attempt of using the scanner Scanner brand = new Scanner(System.in); Scanner BookName = new Scanner (System.in); System.out.println("What is the name of the book?"); String BookName = BookName.nextLine(); Commented Feb 28, 2020 at 4:22
  • since you need user input you'll need that scanner code. Commented Feb 28, 2020 at 4:24
  • Take a look at what while loops are: stackoverflow.com/questions/18038533/using-while-loop-in-java Commented Feb 28, 2020 at 4:25
  • Thanks, I have tried that but that only changes the variable how would I also create the object and increase the size of the arraylist Commented Feb 28, 2020 at 4:26

1 Answer 1

1

What you want to do, conceptually, is capture in the input from the user (e.g., the name of the book), then use that data to instantiate a new Real_Entertainment instance (if that's all you need), then add the new instance to your existing list. For example:

Scanner scanner = new Scanner(System.in);
System.out.print("What is your book's title? ");
String title = scanner.next();
Real_Entertainment realEntertainment = new Real_Entertainment(title, null, 0, 0, 0);
print.add(realEntertainment);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks did not think about it that way. Now if I want more information from the user since I have more variables would I change the null to Author
Exactly! That's correct - You need to ask the user for each of the inputs that you care about, then use the variable references to them as parameters in your constructor. So for example, if you already have the code above and add String author = scanner.next();, you then build the new Real_Entertainment(title, author, 0, 0, 0)
Sure thing! Best of luck!
I just had a thought what if I user types in multiple objects how would I create a structure that creates new objects instead of me pre making an object.

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.