-1

I am making a small programme, part of which iterates over an arraylist sorting the different competitors into a map depending on abilty of each competitor in the array list. It seems to work apart from one problem. The first if statement is not working. Any competitor classed as beginer is not sorted into beginerResults, but instead standardResults. The others are working fine however. Any suggestions?

public void sort()
  {
    for (Golfer rating : competitors)
    {

      if (competitors.getScore()== "beginer")
      {
        beginerResults.put(competitor.getName());  
      }
      if (competitors.getScore()== "Intermediate")
      {
        intermediateResults.put(competitor.getName());  
      }
      else
      {
        expertResults.put(age.getName());
      }
    }
  }
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • 1
    You're comparing strings wrong, see the link provided by ZouZou. Also, there are no Maps in your example, a `Map.put` requires a `key` and a `value`, perhaps you mean `Set`? – Taylor May 06 '14 at 21:30
  • You never use the variable (`rating`) for the current iteration, just the collection (`competitors`) you are iterating over. – David Conrad May 06 '14 at 21:55

1 Answers1

0

Change the String comparisons to

competitors.getScore().equals("beginer")

or even better to

"beginer".equals(competitors.getScore())

Comparing Strings with == is generally not the right way to do this.

Also, beginer is misspelled, check if the calling code has it misspelled
too. If it has it spelled correctly, then that might be your main issue.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159