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

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

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);

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

sato

1 Like

use this indexOf
in stead of equals

2 Likes

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

1 Like

Thank you ! Chrisr !
is the following code correct?

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);
1 Like

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

It looks okay to me

Does it work?

The equals should be okay

but it printed 0 , not 4 (T_T)

This gives an array

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

1 Like

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

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);
2 Likes

Thank you :pray: Chrisir ! I solved the problem successfully!

1 Like

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

https://processing.org/reference/String_equals_.html

:slight_smile:

1 Like

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

When using loadStrings, it should be sato[0]

here is a way to use IntDict

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" );
}

1 Like

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:

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.

Quick update:

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

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

Thank you glv! ill try it!

1 Like

Thank you graysonh! ill try it later!

1 Like

Thank you kll! this is a good idea too!

1 Like