Question: I am trying to model a bag of words. I am having some trouble incrementing the counter inside my dictionary when the word is found in my data (type series):

```
def build_voc(self, data):
        for document in data:
            for word in document.split(' '):
                if word in self.voc:
                    self.voc_ctr[word] = self.voc_ctr[word] + 1
                else:
                    self.voc.append(word)
                    self.voc_ctr = 1

```

I tried indexing it as well this way just to test where the error was:

```
self.voc_ctr[word][0] = self.voc_ctr[word][0] + 1

```

But it still gives me the same error at that line:

> 
> TypeError: 'int' object is not subscriptable
> 
> 
> 

Knowing that this is a function in the same class, where self.voc and self.voc\_ctr are defined:

```
class BV:
    def __init__(self):
        self.voc = []
        self.voc_ctr = {}

    def build_voc(self, data):
        for document in data:
            for word in document.split(' '):
                if word in self.voc:
                    self.voc_ctr[word] = self.voc_ctr[word] + 1
                else:
                    self.voc.append(word)
                    self.voc_ctr = 1

```

The error seems to say self.voc\_ctr is an int object, but I defined it as a list so I don't know where I went wrong.

Answer: 
I presume you realize you're supposed to compare Strings with `equals` and not `==`. However, look at the identity hashCodes for the strings. `abcdef` and `b` have the same one. That is why they are equal using `==` They are referencing the same object from the internal cache.

```
String abcdef = "abcdef";
final String def = "def";
String a = "abc";
a += def;
String b = "abc" + def;
String c = "abc";
c = c + def;
System.out.println("a = " + System.identityHashCode(a)
        + " abcdef = " + System.identityHashCode(abcdef));
System.out.println("b = " + System.identityHashCode(b)
+ " abcdef = " + System.identityHashCode(abcdef));
System.out.println("c = " + System.identityHashCode(c)
+ " abcdef = " + System.identityHashCode(abcdef));
if (abcdef == a) {
    System.out.println("A");
}
if (abcdef == b) {
    System.out.println("B");
}
if (abcdef == c) {
    System.out.println("C");
}

```

Prints

```
a = 925858445 abcdef = 798154996
b = 798154996 abcdef = 798154996
c = 681842940 abcdef = 798154996
B

```
In Java the operator "==" checks if the two operands reference to the same object. Do not use "==" with any objects including Strings. And note Java Strings are immutable. That mean all the operations with them don't modify them but create new objects.