r/javahelp • u/PeeweeTuna34 • Jun 16 '20
Solved How do I print the contents of an array object?
I'm writing a program which utilizes OOP. The program I am creating is supposed to recruit applicants to a team. In my Team.java, I created a method which is supposed to accept members and add it to the team.
In my main.java, everything seems to be working fine. The addMember method works, however when my program is asked to print all the members on the team, only the latest added member is printed.
I tried using a for-loop to add elements to the array but it doesn't do as I expect it to do. It instead prints some kind of cryptic code, something like "Library.@483403".
Can anyone assist me into find a solution? Thank you!
4
Jun 16 '20 edited Jun 16 '20
some kind of cryptic code, something like "Library.@483403"
This "cryptic code" is the address.
Use a for loop to print the elements of an array, and call your Member getters that "get" the Member's age and name, eg memberObject.getAge().
Can't you just use an ArrayList instead and use the add() and get() methods?
1
u/PeeweeTuna34 Jun 16 '20
I forgot to include in my post that I'm following a UML Class Diagram provided by my professor. That's the reason why I did not use an ArrayList.
3
u/darksounds Extreme Brewer Jun 16 '20
There's a bug in addMember.
I don't really want to tell you more than that, as you should be able to find it quickly. If you're struggling to locate it, grab a piece of paper and step through the code by hand, only doing EXACTLY what you have written.
2
u/PeeweeTuna34 Jun 16 '20
I checked addMember, I'm not sure which the bug is but I think it's either
members[maxMember] = addMem;or the block of code inside the else statement. I'll try to rewrite it again.2
u/darksounds Extreme Brewer Jun 16 '20
Don't rewrite it, find what's wrong.
You think it might be
members[maxMember] = addMem? Why do you think it might be? Is that what you want to be doing in addMember?2
u/PeeweeTuna34 Jun 16 '20
You think it might be members[maxMember] = addMem ? Why do you think it might be?
I think I found it.
maxMemberisn't initialized.3
u/darksounds Extreme Brewer Jun 16 '20
Close, but not quite.
What does members[maxMember] = addMem do, anyway?
It finds the index "maxMember" in the members array and puts the new member there.
Does anything seem wrong with that sentence?
What happens when you add the second member? Where do you put it?
0
u/darksounds Extreme Brewer Jun 16 '20
There are other problems, too. Couple questions:
public Member members[] = new Member[5]; public int memberCnt = 0;Why 5? Where does that come from? Why are you initializing these two member variables here, but the others in the constructor?
Member[] bBall = new Member[basketball.maxMember]; Member[] vBall = new Member[volleyball.maxMember];Why do these arrays exist? What do you think they do?
Team basketball = new Team("Chicago Bulls", 2, 18, 21); Team volleyball = new Team("Volleyball Team", 3, 17, 19);You define these, and you add Members into them, but when do you check their contents?
My advice overall? Slow down. Walk through your code a little more. You seem to just be writing things and then getting confused. Take your time to figure out what's wrong.
2
u/PeeweeTuna34 Jun 16 '20
We were instructed to create a program that accepts members to a team. The required slots for the basketball team is 2 while the volleyball team is 3. That's where I got the 5 from. Honestly, I did not think it through.
I wanted to make another array that holds the inputs of the users and print the elements.
That is instructed by my professor. I only followed that line of code.
Also, thank you for your advice. I will definitely take those into consideration.
1
u/darksounds Extreme Brewer Jun 16 '20
We were instructed to create a program that accepts members to a team. The required slots for the basketball team is 2 while the volleyball team is 3. That's where I got the 5 from. Honestly, I did not think it through.
Consider deciding how long the array should be inside of the constructor. Could you do that? Do you have enough information at that point in the execution to make an informed decision?
I wanted to make another array that holds the inputs of the users and print the elements.
But why? Isn't that what your Team object is for?
2
u/Jarl-67 Jun 16 '20
Why exactly do you have the bBall and vBall arrays? The OOP way of printing the list of Players (you call these Members) is to either return a list of Players or call the toString method on the Team, e.g. basketball.toString();.
If you have a volleyball team and a new player wants to join but you already have 5 players. What would you do?
1
u/PeeweeTuna34 Jun 16 '20
Why exactly do you have the bBall and vBall arrays? The OOP way of printing the list of Players (you call these Members) is to either return a list of Players or call the toString method on the Team, e.g. basketball.toString();.
Sorry for the late reply. That is what I actually did in the first place but my professor deemed that it was wrong. She told me I wasn't following her instructions. After I asked her about it, she said I need to use Scanner for the input and pass the variables to the Member constructor.
2
0
u/McBashed Jun 16 '20 edited Jun 16 '20
What editor are you using? At the top of your main method file, have this include statement:
import java.util.Arrays;
Then when you want to print it you can type System.out.println(Arrays.toString(foo)) and it will print it to the console. This is because the toString method needs to be overloaded depending on how you are using it. When you type System.out.println(foo), it prints the memory location of the Array - also known as some kind of cryptic code OR a hex valued memory address in some other peoples worlds. Don't worry too much about these values right now
import java.util.Arrays;
public class Main {
public static void main(String[] args) {int foo[] = {1,5,7,10};
System.out.println(Arrays.toString(foo)); } }
Note that Arrays package is part of the JDK, so you don't need to source an external package for this - it is automatically "included" but you need to tell the computer where it is at that top statement. Some people just do this but I consider it bad practice at this point:
include java.util.*; The * is the wildcard operator, so this statement means import all java utilities. Imo learn what you need, why you need it, how to use it, and only import that stuff.
Alternatively use an ArrayList<type>, they can be printed natively:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> bar = new ArrayList<Integer>(2);
bar.add(1);
bar.add(2);
System.out.println(bar);
}
}Sorry kinda went off on a tangent there, but if you have any addt'l questions feel free to respond or pm me
1
u/PeeweeTuna34 Jun 16 '20
Thanks for the help! I did try an arraylist and it somehow managed to print the desired outputs
And I use netbeans as my editor btw
1
u/McBashed Jun 16 '20
ArrayList will print its contents, not the memory location which is why it worked - fyi
9
u/bean_factory Jun 16 '20 edited Jun 16 '20
@tinatamoranmoako is correct that the "proper" way to print an array is to loop over its contents and print out each element's toString() value. However printing the contents of an array is a fairly common problem, so the built-in method
Arrays.toString(foo)can save you from reinventing the wheel for this common task.EDIT: Also, a UML diagram is a high-level representation of how a program's substituent parts interact. I'm 99.99% certain that your professor isn't concerned about the choice of List vs ArrayList (a List implementation) vs array[], but only about the relationship between components' visibility ("what 'they' can see") and cardinality ("how many others can 'they' see"). UML diagrams work best when you focus on the big idea and don't get too bogged down in details (because the diagram author probably didn't either :P).