0

I would like to count the value of elements in my array.

Example:
Elements in the Array

a={"AB", "CD", "EF", "CD", "CD", "GH", "EF"}

so the counter for CD should be 3.

Thats what I tried. I think the main Problem is that I have no Array where the Elements are stored.

public int countValue(final String[] strings, final String value) 
    {
        int counter=0;


                for(int i = 0; i<strings.length; i++)
                {
                    if(value==strings[i])
                    {
                        counter++;
                    }
                }
                return counter;
    }
}
Asif Raza
  • 3,435
  • 2
  • 27
  • 43
Lari Fari
  • 11
  • 2
  • 1
    *I have no Array where the Elements are stored.*?. Well, then create one. Also, use `equals()` to compare Strings. I don't know how you are trying to fix this issue (you will have duplicates in your current code) – TheLostMind May 10 '17 at 10:04
  • 2
    `==` goes to `.equals` and you're pretty much there. – Bathsheba May 10 '17 at 10:04
  • The answer can be found by looking at the "duplicate" I've spotted. Do study it carefully. – Bathsheba May 10 '17 at 10:06
  • Compare String using `String.equals()` and not `==` – msfoster May 10 '17 at 10:06
  • 1
    The "duplicate" is only a part of the solution. the whole solution is the comment of @TheLostMind – Jens May 10 '17 at 10:13
  • @Jens - It was either a dupe hammer or "too broad" :(. The OP should probably read about `Map`s and `Set`s too – TheLostMind May 10 '17 at 10:16

1 Answers1

0

You need to use the .equals() method to compare two strings. The == operator checks whether both are the same object

thardes2
  • 1,162
  • 12
  • 28