简体   繁体   中英

“Cannot find symbol” - class with the main method can call methods from one of the other classes but not the second of the others?

I've been lurking here for a little while, but I've come into a problem that I can't solve in some Java programs I'm writing for an assignment. I bet they're not too difficult to figure out, but I'm just not getting it.

I've been getting errors along the lines of this:

RugbyTeamLadderEditor.java:125: cannot find symbol
symbol  : method findAveragePoints(java.util.ArrayList<RugbyTeam>)
location: class RugbyTeamLadderEditor
                        double averagePointsToBePrinted = findAveragePoints(rugbyTeams);

I have three classes, and, from the class with the main method (RugbyTeamLadderEditor), I can call the constructor class, but not the other class which has some methods in it (Part1). Should I be doing something with packages? - all I know is that I didn't learn anything about packages in this introductory programming course that I'm doing, and I'm not sure how they would be received if I were to use them.

My code is a couple of hundred lines long, so I put them in pastebin - I hope I haven't transgressed any faux pas by doing this :/ Every class is in its own .java file.

http://pastebin.com/FrjYhR2f

Cheers!

EDIT: a few fragments of my code:

In RugbyTeamLadderEditor.java:

// if the identification number is equal to 5, then print out the average points of all of the teams in the ArrayList
    else if (identificationNumber == 5)
    {
        double averagePointsToBePrinted = findAveragePoints(rugbyTeams);
    }

In Part1.java:

/**
 * This method takes a RugbyTeam ArrayList and returns a
 * double that represents the average of the points of all
 * of the rugby teams
 */
public static double findAveragePoints(ArrayList<RugbyTeam> rugbyTeams)
{
    // If there are no objects in the ArrayList rugbyTeams, return 0
    if (rugbyTeams.size() == 0)
        return 0;

    // Declare a variable that represents the addition of the points of each team;
    // initialise it to 0
    double totalPoints = 0;

    // This is a code-cliche for traversing an ArrayList
    for (int i = 0; i < rugbyTeams.size(); i++)
    {
        // Find then number of points a team has and add that number to totalPoints
        RugbyTeam r = rugbyTeams.get(i);
        totalPoints = totalPoints + r.getPoints();
    }

    // Declare a variable that represents the average of the points of each teams, 
    // i.e. the addition of the points of each team divided by the number of teams 
    // (i.e. the number of elements in the ArrayList); initialise it to 0
    double averagePoints = totalPoints / rugbyTeams.size();
    return averagePoints;

}

It's not quite finished yet - I still need to put a print statement in to print that double, but it's irrelevant for now because I can't actually get that double to take on a value.

Your are trying to call the method findAveragePoints . With the current implementation you say, that the method will be found in the class RugbyTeamLadderEditor . But the method is defined in the class Part1 . So to make this work you prepend the call to the method with Part1. (since it is a static method) and the program should work.


EDIT

The code would basically look like this

double averagePointsToBePrinted = Part1.findAveragePoints(rugbyTeams);

Also every time you try to call a method that is defined in another class than the current you either have to provide an instance of this class or prepend the name of the class (like here Part1 ) to the method called.

As a side node you should change the name of your variable quitProgram . The name of the variable and its meaning contradict each other. So to make things more clear for anyone reading the code you should change either the name or the handling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM