John123 John123 - 29 days ago 4
Java Question

Why is this not outputting my user's properties in this ArrayList?

So the program specifies no errors but it isn't outputting what I want it to. I've made an Array List which should output a user's details which are a username, user Type and a name. I've called the class in my main method but nothing outputs. I'm new to Java so if somebody could help me with how to fix this so that it compiles the user's properties then that would be of great help to myself and possibly other beginners who may come across the same issue. Here is the code:

UserGroup class (where ArrayList is created

package main;
import java.util.ArrayList;

public class UserGroup {

ArrayList<User> userGroup = new ArrayList<>();
User userOne;

public void addSampleData(String username, String userType, String name) {

userGroup.add(new User("LeeB123", "Staff", "Lee"));

}

public User getUser(int index) {

return userGroup.get(0);

}

public void printusername(){

for (User x : userGroup) {
System.out.println(x);
} }
}


User class

package main;
class User {

String username;
String userType;
String name;


User(String username, String userType, String name) {

this.username = username;
this.userType = userType;
this.name = name;
}

public String getUsername() {
return username;
}

public String getUserType() {
return userType;
}

public String getName() {
return name;
}

public String setUserType(String admin) {
return userType = admin;
}

}


Main class

package main;

public class Main {
public static void main(String[] args) {

public User callUserGroup; {

UserGroup userGroupObject = new UserGroup();

System.out.println(userGroupObject.getUser(0));
}

}


I understand some parts may be irrelevant/not efficient or necessary but I'm a beginner so I'm still getting use to the concepts so some help would be greatly appreciated, thanks.

Answer

I will break down some problems for the UserGroup class then the main.

UserGroup Class: Basically this is a GROUP of Users. So the only property you need is the ArrayList of Users. The main thing missing here is a constructor. You have no way to create a new UserGroup Object. I made a constructor that takes nothing and adds some data to the userGroup. In the addSampleData method you are taking in three string variables, then create a new User object with those variables and add it to userGroup. Since the userGroup Array is made up of User objects, I changed the incoming variables from three string variables to one User variable. I changed the name addSampleData method to addUser. The changes below should address the issues just described.

Edit: Added missing getUserGroup() property getter to UserGroup class.

ArrayList<User> userGroup = new ArrayList<>();
// User userOne;
public UserGroup()
{
  addUser(new User("LeeB123", "Staff", "Lee"));
  addUser(new User("XLeeB123", "XStaff", "XLee"));
  addUser(new User("XXLeeB123", "XXStaff", "XXLee"));
}

public void addUser(User inUser)
{
  //userGroup.add(new User("LeeB123", "Staff", "Lee"));
  userGroup.add(inUser);
}
public ArrayList<User> getUserGroup() {
    return userGroup;
}

Main method: Since the userGroupObject has an array of User objects I used a for each loop to output all the Users in the userGroupObject.

//public User callUserGroup;   
UserGroup userGroupObject = new UserGroup();

for (User curUser : userGroupObject.getUserGroup())
{
   System.out.println(curUser.toString()); 
}

EDIT 2: toString method added to User class.

@Override
public String toString() {
    return "User username=" + username + ", userType=" + userType + ", name=" + name;
}

Hope this helps.

Comments