How would I add a object to a arraylist using scanner?

TheGamer27 :

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    
    }

}
Bill Horvath :

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);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How would I assign values to an object on creation, that takes String and ArrayList values as params?

how should i add an object to a private static ArrayList?

How would I find the first 5 multiples of 4, 5, and 6 and add them to an ArrayList?

How would I properly separate a string into an ArrayList?

How to add an object to an ArrayList in Java

ArrayList IndexOutOfBoundsException using Scanner

CQRS/DDD How would add an object to an aggregate using a command?

How would I add margin-bottom using jQuery

How to add object to arraylist

How would I add an "active" class to the rendered images based on object properties?

How would I add timer to carousel sliders using Javascript

How would I be able to Add, Multiply, and Subtract using buttons

Using GUI to add an object to an arraylist

How would I pivot this object using linq?

Not sure how to create an object with Java Scanner and storing it inside an ArrayList

How I can add objects into an ArrayList when I'm using this ArrayList in a for loop?

How do I add a blank line after the scanner object?

How Would I Write This Registry Using 'Reg Add'?

How can I add an object to an arraylist using setState

How to assign an object to another object using Scanner

How to add arrays using a for loop and scanner in Java

How would I properly add an array as a field when creating a new object that has arrays as a parameter?

Using Scanner to add to empty Object

How do I add objects inside an ArrayList using oop?

How would I validate if my JSON object has another object or and Array, using Liquid Template

How would I add multiple attributes together and store them in another attribute in Object Oriented Programming in Python?

Select object via scanner and add it to an arrayList

Using scanner input in enhanced For loop to find indexOf an object in ArrayList

How would I push a object within a object in JavaScript using push function