Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help printing information

 
johnathan smith
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

i have a class "Club"
it contains an ArrayList, add and remove methods and a method to retrieve player details. (The code for class Club is provided below).

i want to write a new class called Main which prints out the information held about each player in the club.
is this possible and if so how would i go about achieving this?

Any help would be greatly appreciated.
Many thanks.

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like you might want to add a method to the Player class (or maybe just override toString) that returns the details. Then add a method to the Club class that iterates through the ArrayList and calls the new method on each Player.
 
johnathan smith
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, its an option
but it the long run it needs to be in a new Class main

anyidea what i can write in main to do this?

thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is remove doing what you want right now? I'm betting not. I suggested some tests the other day ...

Is that how you expect Club to behave?
 
johnathan smith
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the tips
im sorting the remove stuff now

do you have any clue about how to print the information about all the players held in the club? i need to do this inside the new class called main I am writing?

thanks again
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by johnathan smith:
yes, its an option
but it the long run it needs to be in a new Class main

anyidea what i can write in main to do this? ...

I'm sorry -- maybe I don't understand the question. Even if you have a separate class, you're going to need a reference to the ArrayList. And you're going to iterate through the List, and call some method on each Player to get the details you want. Something like...

What am I missing? Can you spotlight where the problems are?
[ April 02, 2007: Message edited by: marc weber ]
 
johnathan smith
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the tips

iv tested the following code
but get a compiel error saying "cannot find symbol - class List"
do you know what the problem can be?
thank you

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by johnathan smith:
thanks for the tips

iv tested the following code
but get a compiel error saying "cannot find symbol - class List"
do you know what the problem can be?...

Add an import statement at the top...

import java.util.List;

Also, that code was just meant as a suggestion. It assumes that your Player class will have a method like getDetails().
[ April 02, 2007: Message edited by: marc weber ]
 
johnathan smith
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey yer iv sorted the import

my player class doesnt have a getDetails method
if i was to write one, would all the player information held in club be printed out using the method you provided?

what sort of information would i have to put in the get detaisl method?

thanks
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by johnathan smith:
hey yer iv sorted the import

What does that mean?

Originally posted by johnathan smith:
...my player class doesnt have a getDetails method
if i was to write one, would all the player information held in club be printed out using the method you provided?

what sort of information would i have to put in the get detaisl method? ...

You need to decide what you want this method to do. You indicated above that you want something that "prints out the information held about each player," so I think you've answered your own question. Have you tried to write this method?
 
johnathan smith
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i havnt started writing the method yet
does my player class need to override the toString() method?
i guess this will return a String that represents all the details of the Players?

if thats correct how do i go about putting it together?

thanks
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by johnathan smith:
i havnt started writing the method yet ... i guess this will return a String that represents all the details of the Players? if thats correct how do i go about putting it together? ...

I really think you should make an attempt at this, instead of us writing the method for you. What do you think the method should look like?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic