How do I take user input and store it into an array that is in another class? || Java

Sharlene Roberts :

I'm trying to take multiple user inputs (which will be strings in this case / names) and store it into an array that is in another class
Based on what I've researched many advise to use a list instead of an array, but in this case an array was specified to be used.
I was able to breakdown the problem by using code that was meant to receive an integer as input and I had made changes to receive a string instead, however, in the case where I entered for example, 3 names were to be entered and the code prompted me to enter the names, only 2 were allowed to be entered
Here's the class where the array was initialized

    public class Movie {
    private String id;
    private String name;
    private String description;
    private String[] genre;
    private String[] actors;
    private String[] language;
    private String countryOfOrigin;
    private Map<String, Integer> ratings;


    //Constructor
    public Movie(String id, String name, String description, String[] genre, String[] actors, String[] language, String countryOfOrigin, Map<String, Integer> ratings){
        this.id = id;
        this.name = name;
        this.description = description;
        this.genre = genre;
        this.actors = actors;
        this.language = language;
        this.countryOfOrigin = countryOfOrigin;
        this.ratings = ratings;
    }

    //setters
    public void setid(String id){
        this.id = id;
    }

    public void setname(String name){
        this.name = name;
    }

    public void setDescription(String description){
        this.description = description;
    }

    public void setGenre(String[] genre){
        this.genre = genre;
    }

    public void setActors(String[] actors){
        this.actors = actors;
    }    

//getters
    public String getId(){
        return id;
    }

    public String getName(){
        return name;
    }

    public String getDescription(){
        return description;
    }

    public String[] getGenre(){
        return genre;
    }

    public String[] getActors(){
        return actors;
    }

Here's the main file with the code for the person to enter the names

public class Main{

    public static void main(String[] args) {
       //Scanner initialization
       Scanner input = new Scanner(System.in);

       System.out.println("How many actors star in this movie?: ");
            int num = input.nextInt();

            String array[] = new String[num];
            System.out.println("Enter the " + num + " actors starred now");

            for (int i = 0; i < array.length; i++){
                array[i] = input.nextLine();
            }
}

My problem is I'm not sure if I need to first store the values into the local array "array" and THEN assign said array to the class Movie actors array, or if I need to directly assign the values into the class Movie actors array. If so, I'm unsure how to do that.
My other problem is the one mentioned above where if I entered 3 names to be stored, it only allows me to enter 2

Arvind Kumar Avinash :

My problem is I'm not sure if I need to first store the values into the local array "array" and THEN assign said array to the class Movie actors array, or if I need to directly assign the values into the class Movie actors array. If so, I'm unsure how to do that.

Do it as follows:

import java.util.Arrays;
import java.util.Scanner;

class Movie {
    private String[] actors;
    private String director;
    private int year;

    Movie() {
    }

    public Movie(String[] actors, String director, int year) {
        this.actors = actors;
        this.director = director;
        this.year = year;
    }

    public String[] getActors() {
        return actors;
    }

    public void setActors(String[] actors) {
        this.actors = actors;
    }

    public String getDirector() {
        return director;
    }

    public void setDirector(String director) {
        this.director = director;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    @Override
    public String toString() {
        return "Movie [actors=" + Arrays.toString(actors) + ", director=" + director + ", year=" + year + "]";
    }
}

public class Main {
    public static void main(String[] argv) throws Exception {
        // Scanner initialization
        Scanner input = new Scanner(System.in);

        System.out.print("How many actors star in this movie?: ");
        int num = Integer.parseInt(input.nextLine());

        String array[] = new String[num];
        System.out.println("Enter the " + num + " actors starred now");

        for (int i = 0; i < array.length; i++) {
            array[i] = input.nextLine();
        }

        System.out.print("Enter the name of the director:");
        String director = input.nextLine();

        System.out.print("Enter the release year:");
        int year = Integer.parseInt(input.nextLine());

        Movie movie1 = new Movie(array, director, year);

        // Alternatively
        Movie movie2 = new Movie();
        movie2.setActors(array);
        movie2.setDirector(director);
        movie2.setYear(year);

        // Display
        System.out.println(movie1);
    }
}

My other problem is the one mentioned above where if I entered 3 names to be stored, it only allows me to enter 2

Use Scanner::nextLine instead of Scanner::nextInt. The problem you are facing is because the new line character (Enter) after the int input is being assigned to array[0]. Check Scanner is skipping nextLine() after using next() or nextFoo()? for more information.

A sample run:

How many actors star in this movie?: 2
Enter the 2 actors starred now
James
Bond
Enter the name of the director:Xyz
Enter the release year:2010
Movie [actors=[James, Bond], director=Xyz, year=2010]

Guess you like

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