Pergunta

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());
      }
    }
  }
Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top