blacksmth
blacksmth

Reputation: 55

creating multiple objects in java oop

i cant just get my head around this. i've got a user class:

public class user {

private String name;
private int age;

public user()
{
}

public String getName()
{
    return name;
}



public int getAge()
{
    return age;
}

public void setName(String kname)
{
    name = kname;
}


public void setAge(int kage)
{
    alter = kage;
}

}

and i'm creating and listing users within my userUI class

public class PersonalUI {

public static void main (String args[]) {
    int menu = 0;
    int i = 0;
    while (menu != 3)
    {   
    System.out.println("Please choose: \n (1) Create User \n (2) List User                \n              (3) Quit");
    menu = Console.readInt();
    if (menu = 1)
    {   
    User user[i] = new User();
    System.out.println("Please enter a name");
    String kname = Console.readString();
    user[i].setName(kname);
    }
}

My Problem: How can I create multiple objects as i'm getting an error for User user[i] = new User(); guess im just doing it wrong

Upvotes: 1

Views: 54067

Answers (5)

exexzian
exexzian

Reputation: 7890

It should be
User user[] = new User[size]; // here you creating array of User objects of size size

And then for each user:
user[i] = new User(); // and here you are initializing each user object

Upvotes: 1

Boris Strandjev
Boris Strandjev

Reputation: 46943

Array is not the way to go in your case. Use List:

List<User> users = new ArrayList<User>;

if (menu == 1)
{   
    User newUser = new User();
    users.add(newUser);
    newUser.setName(kname);
}

You don't know the number of users that will be entered up front - thus use dynamic data structure.

Your problem is that you are trying to do two things in the same go : create an array and assign value to specific element of it.

This is how you should operate an array:

int length = 5; // predetermined by you constant
User []users = new User[length];
//....
users[i] = new User();

Exactly because of the predetermined constant I advise using List.

Upvotes: 9

nageswara rao
nageswara rao

Reputation: 64

You can check this mistake.

User user[i] = new User();

user[i] = new User();   // add this in your code

System.out.println("Please enter a name");
String kname = Console.readString();
user[i].setName(kname);

Many think that the statement, User user[i] = new User(), creates an object of User, but it creates a reference variable. Conversion of reference variable into object is required as follows:

user[i] = new User();

This you can find more in Confusion of reference variables and objects in arrays (array of array objects)

Upvotes: 3

Abubakkar
Abubakkar

Reputation: 15654

Your comparison to check the menu value is wrong.

It shouldn't be menu = 1 instead it should be menu == 1.

I don't know how are you going to store the users but what I suggest is make an ArrayList that will store the user objects and whenever creating a new User object add it to the list.

And while listing the users , just iterate through the array list created.

I think this is what you need:

public static void main (String args[]) {
    int menu = 0;
    int i = 0;
    List<User> list = new ArrayList<User>();
    while (menu != 3)
    {   
    System.out.println("Please choose: \n (1) Create User \n (2) List User                \n              (3) Quit");
    menu = Console.readInt();
    if (menu == 1)
    {   
    User user = new User();
    System.out.println("Please enter a name");
    String kname = Console.readString();
    user.setName(kname);
    // add this user to the list
    list.add(user);
    }
}

Upvotes: 1

PermGenError
PermGenError

Reputation: 46408

First You should read the size of the user array from command line to define the size of your array outside the loop. then inside the loop you populate the array with appropriate elements.

     System.out.println("please enter size of the user");
    int size=console.readInt();
    User[] user = new User[size]
    while (menu != 3)
    {   
    System.out.println("Please choose: \n (1) Create User \n (2) List User                \n              (3) Quit");
    menu = Console.readInt();
    if (menu == 1)//changed, you were using assigning here (menu=1)
    {   
    user[i] = new User();

however, if you want a dynamic array, use a List implementing class like ArrayList which doesn't require you to provide size while creating the array.

   List<User> usersList = new ArrayList<User>();
   //then inside the loop populate the list with its `List#add(elem)` method

Upvotes: 1

Related Questions