Here is my User.java class.

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 :

 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...??

有帮助吗?

解决方案

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.

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

其他提示

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

When you know object of your class User is meaningless without

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

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

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

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

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

You can think of creating a ArrayList or Array of user objects

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

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;

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.

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

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

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

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):

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.

Save the Users in an ArrayList at least as follows:

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. :)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top