Trying to check string against name of array

Let’s consider a specific scenario where the objects to match are animals. For each animal we want to note its habitat and diet. in programming these are called attributes or fields because they describe some feature of the object.

We can create arrays to store habitat and diet details like this

String[] habitat = {"Land", "Air", "Water"};
String[] diet = {"Carnivore", "Herbivore", "Omnivore"};

Now consider an animal object it will have 3 attributes

  1. type : name of the animal
  2. habitat trait : index number to the matching trait in the habitat array
  3. diet trait : index number to the matching trait in the diet array

So each animal requires 3 pieces of data a String for its name and 2 integers for its traits. It will be extremely messy to represent this data using using just primitive data type arrays.

One solution is to use object orientated programming (OOP) and the sketch below does just that.

There are two classes
Animal which stores details of the animal’s name, habitat and diet traits
AnimalMatch which holds details of the result when comparing traits of two animals.

Since you are unfamiliar with classes and objects I have used the most basic OOP syntax that I can get away with. Don’t worry if you can’t follow it all you can always ask questions here.

OOP is a powerful programming paradigm and is well worth the effort to learn the Coding Train has a huge number of tutorial videos, you might start here

If you run the sketch it will create 7 animals and select one at random. It then match it with all animals. Here is an example of the output

###  Animals  ###
Lion     Habitat: Land     Diet: Carnivore
Shark     Habitat: Water     Diet: Carnivore
Eagle     Habitat: Air     Diet: Carnivore
Fox     Habitat: Land     Diet: Omnivore
Seagull     Habitat: Air     Diet: Omnivore
Elephant     Habitat: Land     Diet: Herbivore
Mantee     Habitat: Water     Diet: Herbivore
--------------------------------------------

Hidden animal is a Seagull
  Lion   0 traits matched. 
  Shark   0 traits matched. 
  Eagle   1 traits matched.   # Habitat   
  Fox   1 traits matched.   # Diet   
  Seagull   2 traits matched.   # Habitat   Diet   
  Elephant   0 traits matched. 
  Mantee   0 traits matched. 

Sketch code

// The result from comparing traits of 2 animals
class AnimalMatch {
  int nbrMatches = 0;
  boolean[] matches = new boolean[2];

  String toString() {
    String s = nbrMatches + " traits matched. ";
    if (nbrMatches > 0) {
      s+= "  # ";
      if (matches[0]) s += "Habitat   ";
      if (matches[1]) s += "Diet   ";
    }
    return s;
  }
}

class Animal {
  String type;
  int habitatType = -1;
  int dietType = -1;

  Animal(String type, int tr0, int tr1) {
    this.type = type;
    this.habitatType = tr0;
    this.dietType = tr1;
  }

  AnimalMatch match(Animal a) {
    AnimalMatch m = new AnimalMatch();
    m.matches[0] = (habitatType == a.habitatType);
    m.matches[1] = (dietType == a.dietType);
    // Count the number of traits that match
    for (boolean b : m.matches) m.nbrMatches += b ? 1 : 0;
    return m;
  }

  String toString() {
    String s = type + "     Habitat: " + habitat[habitatType];
    s += "     Diet: " + diet[dietType];
    return s;
  }
}

String[] habitat = {"Land", "Air", "Water"};
String[] diet = {"Carnivore", "Herbivore", "Omnivore"};
Animal[] animals;

void setup() {
  createAnimals();
  // Select an animal at random
  int hidden = floor(random(animals.length));
  println("Hidden animal is a " + animals[hidden].type );
  // Match this animal against all animals
  for (int i = 0; i < animals.length; i++) {
    AnimalMatch am = animals[i].match(animals[hidden]);
    println("  " + animals[i].type + "   " + am.toString());
  }
}

void createAnimals() {
  animals = new Animal[] {
    new Animal("Lion", 0, 0),
    new Animal("Shark", 2, 0),
    new Animal("Eagle", 1, 0),
    new Animal("Fox", 0, 2),
    new Animal("Seagull", 1, 2),
    new Animal("Elephant", 0, 1),
    new Animal("Mantee", 2, 1),
  };
  println("###  Animals  ###");
  for (Animal a : animals) println(a.toString());
  println("--------------------------------------------\n");
}