Question

I have a problem however i'm not quite sure where it lies. I am reading data from a text file, NRL team data to be specific. The data is split up with commas and the data is: club name, club mascot, club alias. There are 20 or so lines of this.

I need to rely on classes, and instantiating a class into an array of objects. the class Club contains club name, club mascot, club alias.

When i try to print to the screen the data in the 'alias' field from the club object from the clubArray array, it only prints 'wests', the very last entry in the data file.

I have commented out the setTeamName and setClubMascot bits of it because i was getting an error, however, the code should still work and put every token into the alias data field, shouldnt it?

it would be greatly appreciated if someone could point out what im doing wrong. Im not sure if the problem is with writing it to the array to getting it from the array or somehwere in between.

public static void loadData(Club[] clubArray) throws FileNotFoundException
{
    int i = 0;
    File myfile = new File("NRLclubs.txt");
    if(myfile.exists())
    {
        Scanner inputFile = new Scanner(myfile);

        // Read until the end of the file.
        while (inputFile.hasNext())
        {
            clubArray[i] = new Club();
            String line = inputFile.nextLine();
            StringTokenizer strToken = new StringTokenizer(line, ",");

            while(strToken.hasMoreTokens())
            {
                /*String teamToken = strToken.nextToken();
                Club.setTeamName(teamToken);

                String mascotToken = strToken.nextToken();
                Club.setClubMascot(mascotToken);
                */

                String aliasToken = strToken.nextToken();
                Club.setClubAlias(aliasToken);
            }
            //System.out.println(Club.getClubAlias());
            i++;
        }
        inputFile.close();

        int k = 0;
        while(k < i)
        {
            Club.displayClubAlias(clubArray[k]);
            k++;
        }

    }
    else
        System.out.println("The file was not found");
}

And these are my setters, getters and the displayClubAlias method:

 public static void setClubAlias(String alias)
{
    clubAlias = alias;
}

public String getClubAlias()
{
    return clubAlias;
}

public static void displayClubAlias(Club c)
{
    System.out.println(c.getClubAlias());
}
Was it helpful?

Solution

You mixed up instance field(method) and static field(method).

// in class Club
public String clubAlias; // no static
public void setClubAlias(String alias) // no static
{
    clubAlias = alias;
}

// in method loadData
clubArray[i].setClubAlias(aliasToken); // not Club 

OTHER TIPS

Getters and setters should be static only in case of the static properties . Some IDEs give warning if you try to define static getters/setters for non static properties .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top