# Trying to check string against name of array

**URL:** <https://discourse.processing.org/t/trying-to-check-string-against-name-of-array/46382>\
**Category:** Beginners\
**Created:** [May 12, 2025, 11:04pm UTC](https://discourse.processing.org/t/trying-to-check-string-against-name-of-array/46382 "2025-05-12T23:04:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ember314](https://avatars.discourse-cdn.com/v4/letter/e/ac91a4/32.png) [@ember314](https://discourse.processing.org/u/ember314)\
**Post date:** [May 12, 2025, 11:04pm UTC](https://discourse.processing.org/t/trying-to-check-string-against-name-of-array/46382/1 "2025-05-12T23:04:37Z")

</div>

Hi all! I’m brand new to the forums, and to processing in general, so my apologies if anything is wrong with my post!

I’m trying to make a game in a similar format to games like Pokedle ([https://pokedle.net/](https://pokedle.net/)), but with other media, where each potential answer has ~5 traits related to them, and guessing one makes the program check it against the correct answer, to see which traits match up. My original plan was to have an array for each option, so if you guessed option 1, for example, the program would check each part of option 1’s array against the correct answer’s array.

Unfortunately, at this point the best way I can seem to find to check if a guess is correct is to manually compare it to every single option, which really isn’t ideal. I’d love to know if there’s a reasonably easy way to automate this.

Thanks for any help you can provide!

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [May 13, 2025, 2:09pm UTC](https://discourse.processing.org/t/trying-to-check-string-against-name-of-array/46382/3 "2025-05-13T14:09:00Z")

</div>

Welcome to the forum.

Unfortunately your problem is not clearly defined so answering it is not going to be easy. I tried the ‘classic’ version of the pokemon game to help me understand the problem.

So the computer selects a pokemon at random. Then the player guesses a pokemon by typing in its name. The computer then compares the attributes (Type 1, Type 2, Habitat …) of the player’s pokemon against the hidden one and displays the results.

This image shows part of the pokemon game and we can see that the hidden pokemon has the following attributes

- **Type 1** : is not FIRE, WATER or POISON
- **Type 2** : has NONE
- **Evolution Stage** : 2
- **Height** : between 60 and 160 cm
- **Weight** : 8.5 15.5 kg

I have ignored the other attributes but we can see that comparing attributes is a much more complex than simply testing for equality.

 ![Screenshot 2025-05-13 at 14.44.35](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/8/6/86ad583940bbf447e79c336f9ad0e772959c3770.png)

Personally I would use object orientated programming but I don’t know if you are confident creating classes and objects so I will stop here for the moment.

The next step is for you to give a more concrete example of the game you are trying to create, the type of attributes / traits being used and so on. Also it would help to know what programming language / Processing mode you are using e.g.

- Processing Java mode
- Python
- p5js

---

<div class="post-metadata">

**Author:** ![ember314](https://avatars.discourse-cdn.com/v4/letter/e/ac91a4/32.png) [@ember314](https://discourse.processing.org/u/ember314)\
**Post date:** [May 13, 2025, 4:15pm UTC](https://discourse.processing.org/t/trying-to-check-string-against-name-of-array/46382/4 "2025-05-13T16:15:30Z")

</div>

Thanks so much for responding, and sorry for the lack of information.

I’m using the java version of processing, and I’ve never worked with objects or classes before, so I’m not sure how to use them.

The game I’m trying to make is essentially the example, but using characters from other media, and with fewer traits. The traits I’m trying to use are also all much more similar to the type trait from the example - either matching or not, not worrying about higher or lower values, since that seemed like it would be more complicated.

---

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [May 13, 2025, 7:38pm UTC](https://discourse.processing.org/t/trying-to-check-string-against-name-of-array/46382/5 "2025-05-13T19:38:56Z")

</div>

I hope I get your problem right:  
You have arrays a and b and you want to know which entries in b are also contained in a.

There are multiple ways to do that but the simples way would be to do a for loop like this that checks if an individual entry is in a list:

```auto
String b[]={"Your","array"};
String element_to_be_checked="Your element";
for(int i=0;i<b.length;i++){
    //To check if two strings are the same always use the equals method
    if(b[i].equals(element_to_be_checked)){
       println("Trait "+b[i]+"matches");
    }
}

```

This can be combined with a for loop to check the the array of traits from what you entered.

I hope this helps.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [May 14, 2025, 2:59pm UTC](https://discourse.processing.org/t/trying-to-check-string-against-name-of-array/46382/6 "2025-05-14T14:59:48Z")

</div>

> [@ember314](#):
>
> The traits I’m trying to use are also all much more similar to the type trait from the example

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

```auto
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](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw) has a huge number of tutorial videos, you might start [here](https://www.youtube.com/watch?v=YcbcfkLzgvs)

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

```auto
### 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**

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

```
