¿Cómo se toma la entrada del usuario y lo almacenan en una matriz que está en otra clase? || Java

Sharlene Roberts:

Estoy tratando de tener múltiples entradas del usuario (que será cuerdas en este caso / nombres) y almacenarla en una matriz que está en otra clase
Basado en lo que he investigado muchos aconsejan utilizar una lista en lugar de una matriz, pero en este caso se ha especificado una matriz para ser utilizado.
Yo era capaz de desglose el problema mediante el uso de código que estaba destinado a recibir un número entero como entrada y que había hecho cambios para recibir una cadena en lugar, sin embargo, en el caso en que entré por ejemplo, 3 nombres debían ser introducido y el código yo le pida que introduzca los nombres, a sólo 2 fueron autorizados a introducirse
Aquí está la clase en la que se ha inicializado la matriz

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

Aquí está el archivo principal con el código de la persona a entrar en los nombres

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

Mi problema es que no estoy seguro si necesito primera tienda de los valores en la "matriz" local de matriz y luego dijo asignar matriz a la matriz actores clase de película, o si necesito asignar directamente los valores en la matriz actores clase de película . Si es así, estoy seguro de cómo hacer eso.
Mi otro problema es la mencionada anteriormente en la que si entré 3 nombres para ser almacenados, sólo se me permite entrar 2

Arvind Kumar Avinash:

Mi problema es que no estoy seguro si necesito primera tienda de los valores en la "matriz" local de matriz y luego dijo asignar matriz a la matriz actores clase de película, o si necesito asignar directamente los valores en la matriz actores clase de película . Si es así, estoy seguro de cómo hacer eso.

Hacerlo de la siguiente manera:

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

Mi otro problema es la mencionada anteriormente en la que si entré 3 nombres para ser almacenados, sólo se me permite entrar 2

Utilizar Scanner::nextLineen lugar de Scanner::nextInt. El problema que se enfrentan es debido a que el carácter de nueva línea (Enter) después de la intentrada está siendo asignado a array[0]. Compruebe escáner se está saltando nextLine () después de ir al lado () o nextFoo ()? para más información.

Un análisis de la muestra:

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]

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=284505&siteId=1
Recomendado
Clasificación