Hi I am new to Java and need some help with a call to a private helper method.
The helper method has a arraylist and char in the argument.

Helper Method signature:
[ private List<aClass1> getDancers(List<aClass1> aList, char c) ]

Here is the call to the above helper method within another method that is not working:
[ List<aClass1> dancers = this.getDancers(dancersList, c); ]

Both methods are in the same class. Although both methods are in aClass2 but use aClass1.

Hope that makes sense!

Edited 6 Years Ago by lisa.wells7: n/a

"not working" in exactly what way? If there's an exception or error message please give the complete text. If the result is wrong, please let us know what you expected and what yuou got.
Is dancersList a non-null instance of List<aClass1>?

Hi,

Sorry by not working I mean when the error is "cannot find symbol - variable dancersList"

dancersList is a arraylist of male and female dancers, that's where the char element comes in "m" for male and "f" for female. Yes it is a instance of aClass1 and I think by non-null you mean the arraylist has values. It contains names of the dancers and their gender.

All I would like to do is use the helper method which contains a list of available dancers and in the new method pair them up. ie males with females.

Thanks for your reply!!

"cannot find symbol - variable dancersList"
means either it's misspelled, or it's declared somewhere else in the program and its scope doesn't extend as far as this statement (eg its declared in another class, or within another method in this class)

OK cool thanks! I think it is declared within the helper method.
But one more question how do I get the statement to work:

Sorry I am new to this but I thought if the signature of the helper method has a arraylist and a char in it's argument that I could just use the helper method name :
[ getDancers(List<aClass1> aList, char c) and say this.getDancers(aList, c); ] as both methods are within the same class?

Yes, the code in your orignal post looks valid, the error message suggests its a problem with accessing dancersList, not with your syntax.
If you call doIt(someVariable); then someVariable needs to be declared somewhere that the call can see it. So, if someVariable is declared within doIt(..) then you can't refer to it outside that method.
If doIt() is an instance method (ie not static) then doIt(); and this.doIt(); are the same thing.
If this isn't clear, maybe you can post more of the code, especially the place where dancerList is declared

Edited 6 Years Ago by JamesCherrill: n/a

Thanks for the help I understand what you are saying.
I have included the code for each class and the methods that I need help with.

public class Dancer
{
   private String name; 
   private String classification; // either "Professional" or "Celebrity"
   private char gender; // 'm' is male, 'f' is female
   private boolean partnered; // true, partnered, false not yet.

   public Dancer(String aName, String aClassification, char aGender)
   {
      super();
      this.name = aName;
      this.classification = aClassification;
      this.gender = aGender;
      this.partnered = false;
   }

public class Competition
{
   /* instance variables */
   private List<Couple> couplesList;
   private List<Dancer> professionalsList;
   private List<Dancer> celebritiesList;
   private int numberInCompetition;



   /**
    * Default constructor for objects of class Competition
    */
   public Competition()
   {
      super();
      couplesList = new ArrayList<Couple>();
      professionalsList = new ArrayList<Dancer>();
      celebritiesList = new ArrayList<Dancer>();
      numberInCompetition = 0;
   }

   private List<Dancer> getUnpartneredDancers(List<Dancer> dancersList, char gender)
   {
      Dancer dancer = null;
      List<Dancer> unpartneredDancersList = new ArrayList<Dancer>();
      for (int index = 0; index < dancersList.size(); index++)
      {
         dancer = dancersList.get(index);
         if ((!dancer.getPartnered()) && (dancer.getGender() == gender))
         {
            unpartneredDancersList.add(dancer);
         }
      }
      return unpartneredDancersList;
   }

the method I need help with:

   public List<Couple> createCouple()
   {
      List<Couple> unpartneredCouples = new ArrayList<Couple>();
     [B] List<Dancer> couplesdancers = this.getUnpartneredDancers(dancersList, gender)[/B];  
   }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

If I can cut in... (sorry!) ... I'm not quite clear on the organization here. What class is createCouple() a method of? I don't see where you have dancersList as a variable, and gender is a field of Dancer.

Regardless of where it goes, I'm assuming there's more that's needed for this method - like a return statement, and some logic to calculate what's being returned. Can you give us the purpose of the method? It's called "createCouple", but it returns a List<Couple>, so it's not clear to me what you want this to do.

Hi,

createCouple() is a method of Competition.
Description of the above method:
* Creates and returns a new instance of Couple or returns null otherwise.
* One dancer in the couple must be a professional and the other a celebrity,
* One dancer in the couple must be male and the other female,
* neither dancer in the couple should be partnered already.
* Sets the dancers involved in the couple partnered instance variable to true.

and the method I need help with is also a method of Competition and needs to use the above method to create a list of sorted unpartnered couples 1st and then I can count and order them and add new couples.

Hope this helps.

So why would I need to declare dancersList if I am just calling to another method's argument. And how would I do that with the below statement?

this.getUnpartneredDancers(dancersList, gender); 

Sorry the helper method I need to fetch is:

   /**
    * helper method to return a list of unpartnered dancers
    * of specified gender from a list of dancers.  - given
    * Can be used for professionals or celebrities
    */
   private List<Dancer> getUnpartneredDancers(List<Dancer> dancersList, char gender)
   {
      Dancer dancer = null;
      List<Dancer> unpartneredDancersList = new ArrayList<Dancer>();
      for (int index = 0; index < dancersList.size(); index++)
      {
         dancer = dancersList.get(index);
         if ((!dancer.getPartnered()) && (dancer.getGender() == gender))
         {
            unpartneredDancersList.add(dancer);
         }
      }
      return unpartneredDancersList;
   }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

WHen you call a method, with arguments in the parens, YOU have to supply the arguments. Ie in
getUnpartneredDancers(dancersList, gender);
YOU have to supply the dancersList and the gender variables.

(I'm guessing here, but...) I would expect your app to have somewhere a "master" list of all the Dancers, from which you can extract sub-lists of unpartnered Dancers(etc). Where is the master list?
Afterthought:
Is the "master" list actually one or more of couplesList, professionalsList, celebritiesList ? - in which case perhaps what you shoiuld be doing is calling
getUnpartneredDancers(professionalsList, 'M'); to get all the male professionals, and
getUnpartneredDancers(celebritiesList , 'F'); to get all the female celebrities (etc)

Edited 6 Years Ago by JamesCherrill: Added afterthought

It worked hurray!!!

That's great thanks, I get it now.
Thanks for your time!!


Lisa

So why would I need to declare dancersList if I am just calling to another method's argument. And how would I do that with the below statement?

this.getUnpartneredDancers(dancersList, gender);

Method calls and method headers- two very different things that look similar. I think that's where your confusion is. (there's another thread where the same confusion is happening, I think it's sunshineSerene's one, you might take a look there - this will cover similar ground though)

A method declaration has a signature and a body. (I think the JLS says "header" instead of signature, but bearwith me)

The signature is where you tell the compiler what it needs to know about the method from the outside: who can access it, what it returns, what it's called, and what it expects. The body is where you tell the compiler what to do when the method is called. In the header, you have your formal parameters - these are the bits in the parentheses. These are basically local variable declarations. The items in the parameters have method scope, and they take their values from the arguments passed to the method. So these are local variables which get their values from somewhere outside of scope, and only exist within scope.
That's your list<Dancer> dancerslist and your char gender - in your nethod header, these tell the compiler "this method needs a list of Dancers and a char, and for purposes of this method, those will be called dancersList and gender".

A method call looks a lot like a method header - intentionally so.

this.getUnpartneredDancers(dancersList, gender); // "this" is unneccessary here

this tells the compiler, "call the method getUnpartneredDancers, give it the variables called dancersList and gender, and do nothing with the result".
Now unless this is a recursive call from the method getUnpartneredDancers, the local variables of that method are not avilable to you. You need a List and a char that are in the scope of the calling method, not the method being called.

So that's why you need a List<Dancer> called dancersList and a char called gender for that method call to work.

It might help if you take a sheet of paper and diagram the flow of control from method to method. You start in main, so draw a box labelled "main" and then try to map out the order of the calls and returns - return of control, and return of information. (so if getUnpartneredDancers returns a list of Dancer, and that list is important to you, you want the call to look like this:

List<Dancer> unpartneredDancers = getUnpartneredDancers(dancersList, gender);

in order to capture something from the method)

I think this might not be my clearest writing ever, I was interrupted by actual work. Let me know if I confused you and I'll try again.


As an aside, you might want to replace the Strings and chars with inherently finite and discrete values where those are more suitable. "char gender" makes more sense to me as a boolean "male", "String classificiation" would be another boolean, or if it's possible you might have another type you could have an int, and some constants to keep track.
Something like this:

private int classification;
public static final int PROFESSIONAL = 0;
public static final int CELEBRITY= 1 ;
public static final int AMATEUR= 2;  // maybe this gets added later

This is not just a matter of style, it will make your life easier. For example, being able to use boolean operators (especially the ^, in this case) will simplify your expressions considerably, and help to get the language out of your way.

^ agree, except that the right way to do professional/celebrity etc is an enum. Also, I'm pretty sure there's something in the coding standards about not using booleans unless it's a clear is/isnot situation, eg is !male just female, or maybe gay?. Once again an enum is the safe answer.

Perhaps boolean wantsMalePartner would be the thing. :)

On enums, I don't usually use them unless I want all of the machinery they provide. If I'm just making a set of discrete values, I prefer to use constant ints. However, yours may well be the better answer - my preferences are not always correct!

Hi Jon,

Thanks for the description it was very clear and detailed.
Perhaps the boolean would be better but the assignment I am doing is very specific as to what they want but I will keep that in mind.

Thanks again for your time.

Lisa

This article has been dead for over six months. Start a new discussion instead.