vendredi 20 juillet 2018

Array Loop is printing a reference instead of a string

I have been provided with an object and several methods to work with it. I am having a tough time printing the string I am assigning to the variables. At the moment, I am unsure if I assigning new values at all and I am unable to print any values. Previous iterations have only printed references.

Question: Am I assigning new string values? How do I print a string with the given methods?

Here is the code I was provided

public class Team implements Comparable<Team> {

public String toString(String team, int wins) {
String winningStatement = team + ": " + wins;
return winningStatement;
}

 // Data fields
private String name;
private int winCount;

Team() {
name = "Sooners";
winCount = 1;
}

Team(String inputName) {
name = inputName;
winCount = 1;
}

Team(String inputName, int inputWinCount) {
name = inputName;
winCount = inputWinCount;
}

Here is my current code

   Scanner scnr = new Scanner(System.in);  
        Random rando = new Random();
        String name = "no";
        int cycles = 0;
        int value = 0;
        int match = 0;
        ArrayList<Team> teams = new ArrayList<Team>();
        Team newTeam = new Team(name,1);
        System.out.println("Welcome to the Advanced Sportsball Tracker!");

        while (!name.equals("x")) // looping print statement
        { // x loop begins
            System.out.println("Which team just won? (x to exit)");
            match = 0;
            cycles++;
            name = scnr.next();
            for (Team list : teams)
            {
                if (list.getName().equals(name)) // compares the name of the team to the input value
                    {
                        match++;
                    }
            }
            if (match == 0)
            {
                teams.add(newTeam);
            }

        }// x loop ends
        System.out.print(newTeam.getName());
        if (cycles == 1) // prints no data if user immediately exits
        {
            System.out.println("No data input");
        }
        if (cycles > 1) 
        {
        System.out.println("Final Tally: "); //  loop to print final Talley
        for (Team list : teams) // FIXME
        {
            list.toString(list.getName(),list.getWinCount());  // makes a string out of the string and wincount of the team
        }

Aucun commentaire:

Enregistrer un commentaire