# I want to count the number of occurrences of a name of "sato"

**URL:** https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257
**Category:** Coding Questions
**Created:** [June 24, 2019, 7:27am UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257 "2019-06-24T07:27:23Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![henry](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@henry](https://discourse.processing.org/u/henry)
#### Post date: [June 24, 2019, 7:27am UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257/1 "2019-06-24T07:27:23Z")

</div>

what’s wrong with my code? It should be print out “4” for “sato”'s appearance times

```auto
String[]lines= loadStrings("data.txt");
String[]sato= loadStrings("keyword.txt");

int count=0;
for (int i=0; i<lines.length; i++) {
  String[]tokens=split(lines[i], ",");

  for (int j=0; j<tokens.length; j++) {
    if (tokens[j].equals(sato)) {
      count++;
    }
  }
}
println(count);

```

 ![55](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/9053ad3d0146b94d635829073c492d2566c18b04.png)

> sato,tanaka,yoshida,kida  
> saito,kawada,tanaka,sato  
> mizuno,hirayama,sato,kida  
> utsunomiya,sato,kawada

 ![26](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/6eba1b462a84d9fd9d486629b4565e97673f5d09.png)

> sato

---

<div class="post-metadata">

### Author: ![matheplica](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/matheplica/32/21035_2.png) [@matheplica](https://discourse.processing.org/u/matheplica)
#### Post date: [June 24, 2019, 8:04am UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257/2 "2019-06-24T08:04:37Z")

</div>

use this [indexOf](https://processing.org/reference/String_indexOf_.html)  
in stead of equals

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 24, 2019, 10:50am UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257/3 "2019-06-24T10:50:02Z")

</div>

You are doing it wrong

z is always hence you check only the first line

Before the split statement you need an outer for loop to loop over the lines with z

Then the inner for loop iterates over the components of the current (z) line

It’s called a nested for loop, a loop inside a loop. Think of a grid.

Chrisir

---

<div class="post-metadata">

### Author: ![henry](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@henry](https://discourse.processing.org/u/henry)
#### Post date: [June 24, 2019, 12:08pm UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257/4 "2019-06-24T12:08:40Z")

</div>

Thank you ! Chrisr !  
is the following code correct?

```auto
String[]lines= loadStrings("data.txt");
String[]sato= loadStrings("keyword.txt");

int count=0;
for (int i=0; i<lines.length; i++) {
  String[]tokens=split(lines[i], ",");

  for (int j=0; j<tokens.length; j++) {
    if (tokens[j].equals(sato)) {
      count++;
    }
  }
}
println(count);

```

---

<div class="post-metadata">

### Author: ![henry](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@henry](https://discourse.processing.org/u/henry)
#### Post date: [June 24, 2019, 12:10pm UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257/5 "2019-06-24T12:10:56Z")

</div>

Thank you matheynen!  
I changed the program code,  
I still want to use the equals to solve them…  
is it right?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 24, 2019, 12:58pm UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257/6 "2019-06-24T12:58:38Z")

</div>

It looks okay to me

Does it work?

The equals should be okay

---

<div class="post-metadata">

### Author: ![henry](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@henry](https://discourse.processing.org/u/henry)
#### Post date: [June 24, 2019, 2:59pm UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257/7 "2019-06-24T14:59:07Z")

</div>

but it printed 0 , not 4 (T\_T)

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 24, 2019, 3:49pm UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257/8 "2019-06-24T15:49:35Z")

</div>

> [@henry](#):
>
> Stringsato= loadStrings(“keyword.txt”);

This gives an array

Hence you need to say sato [0] in the line with equals

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 24, 2019, 4:05pm UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257/9 "2019-06-24T16:05:47Z")

</div>

this version below works

maybe you need to use the size() command at the start

use println() to check if the content is loaded correctly

```auto
String[]lines= 
  {
  "sato,tanaka,yoshida,kida", 
  "saito,kawada,tanaka,sato", 
  "mizuno,hirayama,sato,kida", 
  "utsunomiya,sato,kawada", 
};

String sato= "sato";

int count=0;

for (int i=0; i<lines.length; i++) {
  String[]tokens=split(lines[i], ",");

  for (int j=0; j<tokens.length; j++) {
    if (tokens[j].equals(sato)) {
      count++;
    }
  }
}
// ------------------------------------------------------
println(count);

```

---

<div class="post-metadata">

### Author: ![henry](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@henry](https://discourse.processing.org/u/henry)
#### Post date: [June 24, 2019, 5:05pm UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257/10 "2019-06-24T17:05:29Z")

</div>

Thank you 🙏 Chrisir ! I solved the problem successfully!

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [June 24, 2019, 6:17pm UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257/11 "2019-06-24T18:17:40Z")

</div>

Hint:  
.equals(sato);  
is not the same as:  
.equals(“sato”);

[https://processing.org/reference/String\_equals\_.html](https://processing.org/reference/String_equals_.html)

🙂

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 24, 2019, 8:06pm UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257/12 "2019-06-24T20:06:55Z")

</div>

> [@Chrisir](#):
>
> .equals(sato)) {

In my sketch the name of the variable is the same as the content

When using loadStrings, it should be sato[0]

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [June 25, 2019, 1:18am UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257/13 "2019-06-25T01:18:17Z")

</div>

here is a way to use IntDict

```auto
IntDict inventory;
String[] lines, words;
String find = "Lorem";
boolean gotit = false;
long startT, stopT, dT;

void setup() {
  size(200, 200);
  inventory = new IntDict();
  lines = loadStrings("http://websitetips.com/articles/copy/lorem/ipsum.txt");
  for ( String l : lines ) {
    words = split(l, ' ');
    for ( String word : words ) inventory.add(word, 1);
  }
  println(inventory);
  println("we have "+inventory.size()+" entries");
  startT = millis();
  inventory.sortKeys(); // sort alphabetically
  gotit = inventory.hasKey(find); // look for search string
  stopT = millis();
  dT = stopT - startT;
  if ( gotit ) {
    println("Yes, we found _" + find + "_: " + inventory.get(find) + " times");
  } else {
    println("Sorry, no _" + find + "_");
  }
  println(" and needed for sort and search " + dT + " msec" );
}

```

---

<div class="post-metadata">

### Author: ![graysonh](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@graysonh](https://discourse.processing.org/u/graysonh)
#### Post date: [June 26, 2019, 1:57pm UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257/14 "2019-06-26T13:57:56Z")

</div>

Hi Henry,

I’m a little late to answer, but personally I enjoy using regex matching to solve problems like this.

You could try the following:

```auto
String text = join(loadStrings(“data.txt”), ",");
String sato = loadStrings(“keyword.txt”)[0];
String[] matches = match(text, "("+sato+")");
println(matches.length);

```

I haven’t tested this to see if it works for your case, but I think it should. I might try it later when I have access to processing.

---

<div class="post-metadata">

### Author: ![graysonh](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@graysonh](https://discourse.processing.org/u/graysonh)
#### Post date: [June 26, 2019, 3:38pm UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257/15 "2019-06-26T15:38:03Z")

</div>

Quick update:

matchAll is the function I should have been using. The following code works as desired:

```auto
String text = join(loadStrings("words.txt"), ",");
String sato = loadStrings("keyword.txt")[0];
String[][] matches = matchAll(text, "("+sato+")"); 
println(matches.length);

```

---

<div class="post-metadata">

### Author: ![henry](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@henry](https://discourse.processing.org/u/henry)
#### Post date: [June 29, 2019, 10:14am UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257/16 "2019-06-29T10:14:32Z")

</div>

Thank you glv! ill try it!

---

<div class="post-metadata">

### Author: ![henry](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@henry](https://discourse.processing.org/u/henry)
#### Post date: [June 29, 2019, 10:14am UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257/17 "2019-06-29T10:14:59Z")

</div>

Thank you graysonh! ill try it later!

---

<div class="post-metadata">

### Author: ![henry](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@henry](https://discourse.processing.org/u/henry)
#### Post date: [June 29, 2019, 10:16am UTC](https://discourse.processing.org/t/i-want-to-count-the-number-of-occurrences-of-a-name-of-sato/12257/18 "2019-06-29T10:16:02Z")

</div>

Thank you kll! this is a good idea too!
