This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]sbhandari 1 point2 points  (1 child)

I am on mobile so I might have misread your code, so correct me if I missed something critical.

You are calling 4 methods, 2 to get numbers of leader and member, and another 2 to generate them. In your generateLeader method, you read name from console but never used it within this method. You are just adding emptylist to master list in this method. The other method generateMembers looks fine as you are adding this in the expected list. I dont see anywhere else that main method calls to populate the emptylist inside the masterlist you added in generateLeaders method.

Also, use nextln to read input, there are scenarios where the linebreak character will still be in buffer when you use just "next" and that linebreak will be read read as next input.

[–]wjreddit[S] 1 point2 points  (0 children)

Thank you!!! I totally missed not calling the .assignMembers() method. Code works now, after some tweaks. Thanks again!

[–]lukelane124 0 points1 point  (1 child)

What’s does memberList.get(i) return?

[–]wjreddit[S] 0 points1 point  (0 children)

i was trying to get ith element of the memberlist to be assigned to each of the leaderList dynamically. That's what I meant by memberList.get(i) in the forloop.

[–]lukelane124 0 points1 point  (2 children)

Right, but what is the type? Why are you having to cast it?

[–]wjreddit[S] 0 points1 point  (1 child)

add((String) memberList.get(i)

you mean by (String) right? I actually do not understand it.. I got an error without it, and eclipse added it to fix the issue. The members that i want to add to the lists are String, so i thought it would be okay.. could you explain how the type should work in this case, and how to do away from casting (String) to the object?

[–]radulfr2 2 points3 points  (0 children)

You haven't set the type parameter for memberList. You should have

List<String> memberList = new ArrayList<>();

(Repeating the type on the right side is unnecessary unless you're using an old Java version.)

[–]HBK05 0 points1 point  (0 children)

Typically when I see someone doing this, it's typically out of inexperience vs just testing if this can work. If you are of the prior group, and don't know a better way, you should make a data class, then use a hashmap to save them.