[英]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.