-1

I have been provided with an object and several methods to work with it. I am having a tough time printing the string that 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) {
    return team + ": " + wins;
}

// 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;
}

// ----------------------------------------------------
// Getters and Setters 
/**
 * @return the name
 */
public String getName() {
    return name;
}

/**
 * @param name
 *            the name to set
 */
public void setName(String name) {
    this.name = name;
}

/**
 * @return the winCount
 */
public int getWinCount() {
    return winCount;
}

/**
 * @param winCount
 *            the winCount to set
 */
public void setWinCount(int winCount) {
    this.winCount = winCount;
}

/**
 * Increments the winCount variable by one for this Team
 */
public void incrementWinCount() {
    winCount++;
}

/**
 * This method allows you to check to see if this Team object has the same
 * name as another Team object.
 * 
 * This method allows you to use the contains method in ArrayList to see
 * if any element in an array list has the same name as a specific Team.
 * 
 * @param o
 *            the other Team being compared to.
 */
@Override
public boolean equals(Object o) {
    return name.equals(((Team) o).name);
}

/**
 * This method allows you to check to see if this Team object has the same
 * name as another Team object
 * 
 * @param otherTeam
 *            one team
 */
public boolean sameName(Team otherTeam) {
    return name.equals(otherTeam.name);
}
/**
 * This method allows you to check to see if this Team object has the same
 * name as another Team object
 * 
 * @param team1
 *            one team
 * @param team2
 *            the other team
 */
public static boolean sameName(Team team1, Team team2) {
    return team1.name.equals(team2.name);
}

/**
 * This method allows you to sort an ArrayList of Team items using
 * Collections.sort
 * 
 * @param o
 *            the other Team being compared to.
 * @return -1 if this Team item should come first, +1 if this Team item
 *         should come after the other, and 0 if this Team item is
 *         equivalent to the other.
 */
@Override
public int compareTo(Team o) {
    if (this.winCount < o.winCount) {
        return -1;
    } else if (this.winCount > o.winCount) {
        return 1;
    }
    return 0;
}

}

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
        }
  • You first fix the `public String toString()` method that you've given Team. It needs to match the method signature of Object's toString() method -- meaning it should not have any parameters, and this makes sense since your current method's parameters are completely unnecessary and harmful. – Hovercraft Full Of Eels Jul 21 '18 at 02:52
  • which instance are you referring to? The one inside of the cycle's if-statement or inside the methods I was provided? – George Michael Jul 21 '18 at 02:56
  • Which instance? the method within the Team class that has the comment, "this is the method you will fix". Fix it. – Hovercraft Full Of Eels Jul 21 '18 at 03:01
  • I had already fixed the statement I forgot to omit that statement from the instance. Sorry for the confusion. The statement originally only returned "" and received a String argument. – George Michael Jul 21 '18 at 03:03
  • 1
    *Again* a valid `toString()` method should receive **no** argument. None. Please read [How to override toString() properly in Java?](https://stackoverflow.com/questions/10734106/how-to-override-tostring-properly-in-java) – Hovercraft Full Of Eels Jul 21 '18 at 03:11

1 Answers1

0

What was the original code for the toString method they gave you? Did you add the parameters? A better way to do this would be to let the object use it's data fields inside of the method. Passing in the member variables in your for loop is unnecessary, and just bad code. Instead, you want to do something like this:

public String toString() {
    return name + ": " + wins;
}

And in your loop, if you want to print the results, simply do this:

System.out.print(list.toString());
buckeyeguyy
  • 121
  • 8