Results 1 to 7 of 7
  1. #1
    mcnikal is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Default Accessing a method from another class

    Hello,

    I have an admin class that needs to access a method of another class and I'm unsure how to do it.

    One of the methods in the admin class (DancerAdmin) accesses a .txt file with information in and each line is to be extracted and is to be created as an object of the Dancer class. The information in each line is to then be used to set the variables in the Dancer class.

    To set the values the Dancer class has setter methods which I need to access each time a new object is created while cycling through the .txt file. I'm struggling to access these methods from the DancerAdmin class when I run the relevant method.

    The snippet of code I have from the method in DancerAdmin is

    Java Code:
    while (bufferedScanner.hasNextLine())
             {
                currentLine = bufferedScanner.nextLine();
                lineScanner = new Scanner(currentLine);
                lineScanner.useDelimiter(",");
                  
                
                
                dancer.add(new Dancer());
                Dancer.setName(lineScanner.next());
    I get an error saying non static method setName cannot be referenced from a static content?

    Thanks

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default Re: Accessing a method from another class

    I think you may have a couple of issues.
    I'm assuming that dancer is a List of Dancer objects?
    If so, maybe think about renaming it to dancers, so it's a little clearer.

    On line 9, you are adding a new Dancer object to dancer.
    The problem is, you are doing it in an anonymous fashion, meaning that once you add the new Dancer, you no long have a reference to it to
    be able to update its attributes (name, etc).

    As for the problem you pointed out, the setName method is non-static, meaning that it is not a class method, but rather tied to a particular instance of a Dancer. You are calling the setName method on the Dancer CLASS, rather than on a particular dancer instance.

    You probably want to do something like:
    Java Code:
    Dancer dancerToCreate = new Dancer();
    dancerToCreate.setName(lineScanner.next());
    //set other attributes if there are more
    Then, do the equivalent of line 9, where you actually add the dancer to your list(assuming that 'dancer' was a list)
    Java Code:
    dancers.add(dancerToCreate);
    Last edited by sehudson; 04-01-2014 at 10:08 PM.

  3. #3
    mcnikal is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Default Re: Accessing a method from another class

    Yes you are correct it is a list.

    Thank you I have got it working now, much appreciated.

  4. #4
    mcnikal is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Default Re: Accessing a method from another class

    How would it work the other way around? If I needed another method which instead of adding new objects. It read through the list created in the previous method and based on a variable in each object, located it in another list.

    So I have this so far

    My problem is that instead of extracting the dancer to read from the list I'm creating a new one from scratch which obivously has no value set in the variable Im getting.




    Java Code:
    String age;
    
    for (Dancer eachDancer : dancer)
          {
             Dancer testDancer =  new Dancer();
             
             age = eachDancer.getAge();
             
             if (age = "Junior")
             {
             
              juniorResults.add(testDancer);
             }
             
             else if (age = "Senior")
             
             {
                seniorResults.add(testDancer);
             }
             
             else
             
             {
                standardResults.add(testDANCER);
             }
    Last edited by mcnikal; 04-01-2014 at 10:42 PM.

  5. #5
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default Re: Accessing a method from another class

    With the enhanced for loop that you are using, eachDancer will be a reference to the dancer you are currently working with in the loop.
    You are actually creating a new Dancer object each time the loop runs, when you really just want to evaluate each of the Dancer objects
    in the dancer list, and as the loop runs, eachDancer will reference the next Dancer in the list as it iterates over that list.

  6. #6
    mcnikal is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Default Re: Accessing a method from another class

    Ah right. I think I've got it now, although I'm getting an error message saying its an incomptiable type for the local age variable which I'm confused about. The getter message getAge returns a string, but it stills says incompatible types?

    Java Code:
    String age;
          
          
         tring age;
     
    for (Dancer eachDancer : dancer)
          {
             
              
             age = eachDancer.getAge();
              
             if (age = "Junior")
             {
              
              juniorResults.add(eachDancer);
             }
              
             else if (age = "Senior")
              
             {
                seniorResults.add(eachDancer);
             }
              
             else
              
             {
                standardResults.add(eachDANCER);
             }
             }

  7. #7
    mcnikal is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Default Re: Accessing a method from another class

    Never mind I've figured it out. I'm just being an idiot.

    age = "junior"

    age.equals("junior")

    Thanks for your help

Similar Threads

  1. Replies: 7
    Last Post: 06-18-2012, 10:31 PM
  2. Replies: 5
    Last Post: 12-18-2011, 05:06 AM
  3. Accessing a method in another class
    By denno in forum New To Java
    Replies: 14
    Last Post: 04-07-2011, 06:59 AM
  4. Accessing a method in a different class
    By adjit in forum New To Java
    Replies: 4
    Last Post: 04-06-2011, 11:11 PM
  5. Replies: 8
    Last Post: 03-21-2009, 10:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •