想要使用 Setter Getter 从 Java 中获取用户的多个输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17925019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 19:37:58  来源:igfitidea点击:

Want to take multiple input from user in Java with Setter Getter

javajava.util.scannersettergetter

提问by Vaibhav Jain

Here is my User.java class.

这是我的 User.java 类。

public class User {
private String First_Name;
private String Last_Name;
public String getFirst_Name() {
    return First_Name;
}
public void setFirst_Name(String first_Name) {
    First_Name = first_Name;
}
public String getLast_Name() {
    return Last_Name;
}
public void setLast_Name(String last_Name) {
    Last_Name = last_Name;
}
public User(String first_Name, String last_Name) {
    super();
    First_Name = first_Name;
    Last_Name = last_Name;
}
public User() {
    // TODO Auto-generated constructor stub
}   
}

I want to take input from different users as first name and last name. For that i have class Input.java as :

我想将来自不同用户的输入作为名字和姓氏。为此,我将类 Input.java 设为:

 public class InputLogic {

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

    System.out.println("How many name you want to enter:");
      int num = sc.nextInt();
      int i=0;
      while(i < num){
    User firstname = new User();
    System.out.println(i + "Enter First name:");
    String firsttemp = sc.nextLine();
    firstname.setFirst_Name(firsttemp);
    User lastname = new User();
    System.out.println(i + "Enter Last name:");
    String lasttemp = sc.nextLine();
    lastname.setLast_Name(lasttemp);
    i++;

      }
} 
 }

What wrong with this approach...??

这种方法有什么问题......?

回答by Raji

read a nextLine after the nextInt. The return character is else read as the input to the first name.

在 nextInt 之后读取 nextLine。返回字符被读取为名字的输入。

回答by Aniket Thakur

When you know object of your class User is meaningless without

当你知道你的类 User 的对象没有意义时

private String First_Name;
private String Last_Name;

why not put it inside the constructor?

为什么不把它放在构造函数中?

public User(String fname, String lname) {
    this.First_Name = fname;
    this.Last_Name = lname;
}  

then create your object after reading fname and lname from std input as

然后在从 std 输入读取 fname 和 lname 后创建您的对象

User[] userList = new User[num];
for(int i=0;i<userList.length();i++) {
  userList[i] = new User(fname,lname);
}

回答by Charu Khurana

Multiple things incorrect in your InputLogic class. Couple of them:

您的 InputLogic 类中有多项错误。他们中的几个:

1.You're creating numerous firstname, lastnameobjects and then loosing references to it.

1.您正在创建许多firstname,lastname对象,然后失去对它的引用。

2.Why do you need different object for firstname, lastname. Shouldn't they belong to one person (same object)

2.为什么你需要不同的对象firstnamelastname。他们不应该属于一个人(同一对象)

You can think of creating a ArrayListor Arrayof user objects

您可以考虑创建一个ArrayListArray用户对象

回答by ThisAndThat

You are creating two different users (called firstname and lastname) instead of one user containing both names. Try something like:

您正在创建两个不同的用户(称为 firstname 和 lastname),而不是一个包含这两个名称的用户。尝试类似:

while(i < num){
System.out.println(i + "Enter First name:");
String firstname = sc.nextLine();
System.out.println(i + "Enter Last name:");
String lastname = sc.nextLine();
User user = new User(firstname, lastname);
i++;
}

And also your user class needs to contain the strings:

而且您的用户类需要包含字符串:

private String First_Name; private String Last_Name;

私人字符串名字;私人字符串姓氏;

回答by Loki

There are multiple errors on your approach:

您的方法有多个错误:

User xy = new User() //creates a new user

You're doing it for Firstname and for Lastname which is wrong.

您正在为 Firstname 和 Lastname 这样做,这是错误的。

Also you're creating your instances in your while-loop. After this loop ends youre Instances will be gone.

此外,您正在 while 循环中创建实例。此循环结束后,您的实例将消失。

For saving multiple users you could use an array

为了保存多个用户,您可以使用数组

After checking how many Users you want to insert you could create an Array

检查要插入的用户数量后,您可以创建一个数组

User[] users = new User[UserInput]

and in your while-loop you can check for the input and do the following

在你的while循环中,你可以检查输入并执行以下操作

users[i] = new User();
users[i].firstname = firstnameinput;
users[i].lastname = lastnameinput;

回答by ClaireG

You are creating two instances of User; in the first one you are entering only the first name and in the second one you are entering only the last name. Correct your code as follows in order to enter both the name and last name for a single instance of User ( that is one person):

您正在创建两个 User 实例;在第一个中,您只输入名字,在第二个中,您只输入姓氏。如下更正您的代码,以便为单个用户实例(即一个人)输入姓名和姓氏:

public class InputLogic {

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

    System.out.println("How many name you want to enter:");
    int num = sc.nextInt();
    int i=0;
    while(i < num)
    {
        User p = new User();

        System.out.println(i + "Enter First name:");
        String firsttemp = sc.nextLine();

        System.out.println(i + "Enter Last name:");
        String lasttemp = sc.nextLine();

        p.setFirst_Name(firsttemp);
        p.setLast_Name(lasttemp);
        i++;
    }
}

With the code above, however, you are losing the meaning of User p, because you are overriding p's information everytime the loop restarts.

但是,使用上面的代码,您失去了用户 p 的含义,因为每次循环重新启动时您都会覆盖 p 的信息。

Save the Users in an ArrayList at least as follows:

至少按如下方式将用户保存在 ArrayList 中:

public class InputLogic {

public static void main(String args[]) 
{

    ArrayList<User> listOfUsers = new ArrayList<User>();
    Scanner sc = new Scanner(System.in);

    System.out.println("How many name you want to enter:");
    int num = sc.nextInt();
    int i=0;
    while(i < num)
    {
        User p = new User();

        System.out.println(i + "Enter First name:");
        String firsttemp = sc.nextLine();

        System.out.println(i + "Enter Last name:");
        String lasttemp = sc.nextLine();

        p.setFirst_Name(firsttemp);
        p.setLast_Name(lasttemp);
        listOfUsers.add(p);

        i++;
    }
}

In the ArrayList, you now have all the Users saved, and you can access them anytime, or even save them to a database if needed. :)

在 ArrayList 中,您现在保存了所有用户,您可以随时访问它们,甚至在需要时将它们保存到数据库中。:)

回答by noisy cat

The first thing that I noticed is that you use while with iterator i instead of normal for loop. Secondly, if you want to use any object outside of the loop you have to declare it before. Are you sure you want to have two separate users for last name and first name? The loop also suggests you want to have multipe users, so you would need an array or list for that.

我注意到的第一件事是你使用 while 和迭代器 i 而不是普通的 for 循环。其次,如果你想在循环之外使用任何对象,你必须先声明它。您确定要为姓氏和名字设置两个单独的用户吗?该循环还建议您希望拥有多个用户,因此您需要一个数组或列表。

Here is your code corrected.

这是您更正的代码。

public class InputLogic {

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        User users[];

        System.out.println("How many name you want to enter:");
        int num = sc.nextInt();
        users = new User[num];

        for (int i=0; i<num; i++) {
            users[i] = new User();
            System.out.println(i + " Enter First name:");
            String temp = sc.nextLine();
            users[i].setFirst_Name(temp);
            System.out.println(i + " Enter Last name:");
            temp = sc.nextLine();
            users[i].setLast_Name(temp);
        }
    } 
}