简体   繁体   English

如何获取用户输入并将其存储到另一个类中的数组中? || 爪哇

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

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我能够通过使用旨在接收整数作为输入的代码来解决问题,并且我进行了更改以接收字符串,但是,例如,在我输入的情况下,将输入 3 个名称和代码提示我输入名称,只允许输入 2 个
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.我的问题是我不确定是否需要先将值存储到本地数组“数组”中,然后将所述数组分配给类 Movie 演员数组,或者是否需要直接将值分配到类 Movie 演员数组中. 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我的另一个问题是上面提到的,如果我输入了 3 个要存储的名称,它只允许我输入 2 个

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.我的问题是我不确定是否需要先将值存储到本地数组“数组”中,然后将所述数组分配给类 Movie 演员数组,或者是否需要直接将值分配到类 Movie 演员数组中. 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我的另一个问题是上面提到的,如果我输入了 3 个要存储的名称,它只允许我输入 2 个

Use Scanner::nextLine instead of Scanner::nextInt .使用Scanner::nextLine而不是Scanner::nextInt The problem you are facing is because the new line character (Enter) after the int input is being assigned to array[0] .您面临的问题是因为int输入后的换行符 (Enter) 被分配给array[0] Check Scanner is skipping nextLine() after using next() or nextFoo()?检查扫描仪是否在使用 next() 或 nextFoo() 后跳过 nextLine()? 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]

You can approach this problem in many ways, since you declared the array actors private you will need to use a getter and setter method to set and get the data.您可以通过多种方式解决此问题,因为您将数组 actor 声明为私有,您将需要使用 getter 和 setter 方法来设置和获取数据。 You also are missing a constructor.您还缺少构造函数。

 public class Movie
{
    private String[] actors;

public Movie(String[] actors)
{
    this.actors = actors;
}
public String[] getActors()
{
    return actors;
}

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

Then you can simply create a new object Movies and put in your array like this:然后你可以简单地创建一个新对象 Movies 并像这样放入你的数组:

 Movies movies = new Movies(array);
 System.out.println(Arrays.toString(anime.getActors()));

to get all the actors stored in your array.获取存储在数组中的所有演员。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java JOptionPane:如何在字符串中获取用户输入并将其存储在数组中? - Java JOptionPane: How to take an user input in String and store it in an array? 如何从一个 class 中的扫描仪获取用户输入,然后将其应用于同一 package 中的另一个 class? - How do I take an user input from Scanner in one class and then apply it to another class in the same package? 如何获取用户输入并将其显示在阵列上? - How do I take user input and show it on my array? 在Java中,如何获取2D数组的值并将它们存储在具有不同列和行大小的另一个2D数组中? - In Java, how do I take the values of a 2D array and store them in another 2D array with different column and row size? 如何使用 java 在数组中获取用户输入? - how to take user input in Array using java? 如何获取用户输入并在不同类中的数组列表中搜索用户输入? - How do I take a user input and search through an arraylist in a different class for the user input? 使用 Java Swing,如何获取用户的输入并将其存储以备后用? - Using Java Swing, how do you take an input from a user and store it for later use? 如何将用户输入存储到数组列表中? - How do I store user input into an array list? 如何多次获取用户输入并将其存储在同一个数组列表中? - How to take user input multiple times and store it in the same array list? 如何使用另一个类的实例变量接受用户输入? - How do I accept user input with a instance variable of another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM