Compare words from a text with a list of Enum's

4

I have an Enum that contains a list of values. I have a method where I get a text as a parameter. This method, besides going through Enum, breaks the text by words. The main function of the method is to see if there is any word in the text that is equal to some Enum value. The code is below:

public String checkTipo(String texto) {
        List<TipoPokemon> lista = Arrays.asList(TipoPokemon.values());
        String palavras[] = texto.split(" ");

        for(int i=0 ; i < lista.size() ; i++){
        for (String palavra : palavras){
            String tipo = lista.get(i).getNome();
                if (palavra.toLowerCase().equals(tipo)){
                    return "Olá";
                }
            }
        }

        return "";
    }

My Enum is:

package br.com.pokemax.modelo;

public enum TipoPokemon {

    FIRE("FIRE"),
    WATER("WATER"),
    GRASS("GRASS"),
    ELECTRIC("ELECTRIC"),
    ICE("ICE"),
    DARK("DARK"),
    GHOST("GHOST"),
    FAIRY("FAIRY"),
    PSYCHIC("PSYCHIC"),
    DRAGON("DRAGON"),
    POISON("POISON"),
    GROUND("GROUND"),
    ROCK("ROCK"),
    NORMAL("NORMAL"),
    BUG("BUG"),
    FIGHTING("FIGHTING"),
    STEEL("STEEL"),
    FLYING("FLYING");

    private String nome;

    private TipoPokemon(String nome) {
        this.nome = nome;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

}

I'm trying to debug and in the part where I create the variable type , it returns in foreach and does not check if , I need to compare word with Enum, can somebody help me ?

    
asked by anonymous 16.11.2016 / 15:48

2 answers

5

You do not need to set the name this way because every enum has a name() method that does exactly what its getNome() does.

In addition, every enum has a valueOf(String) method that plays a part of what your checkTipo(String) does.

Your code looks like this:

package br.com.pokemax.modelo;

import java.util.Locale;

public enum TipoPokemon {
    FIRE, WATER, GRASS, ELECTRIC, ICE, DARK, GHOST, FAIRY, PSYCHIC,
    DRAGON, POISON, GROUND, ROCK, NORMAL, BUG, FIGHTING, STEEL, FLYING;

    public static String checkTipo(String texto) {
        String palavras[] = texto.split(" ");
        for (String palavra : palavras) {
            try {
                TipoPokemon tipo = valueOf(palavra.toUpperCase(Locale.ROOT));
                return "Olá";
            } catch (IllegalArgumentException e) {
                // Ignora a exceção e continua no for.
            }
        }
        return "";
    }
}

Just do not understand why the method returns "Olá" or "" instead of returning boolean or TipoPokemon found.

Here's a test for it:

public static void main(String[] args) {
    System.out.println(TipoPokemon.checkTipo("ROCK"));
    System.out.println(TipoPokemon.checkTipo("bug"));
    System.out.println(TipoPokemon.checkTipo("banana"));
    System.out.println(TipoPokemon.checkTipo("banana cereja acerola"));
    System.out.println(TipoPokemon.checkTipo("banana cereja dark acerola"));
}

Here's the output:

Olá
Olá


Olá

See here working on ideone.

    
16.11.2016 / 16:16
2

An option without catching an exception:

public boolean checkTipo(String texto) {
    List<String> palavras = Arrays.asList(texto.toUpperCase().split(" "));

    for (TipoPokemon tipoPokemon : TipoPokemon.values()) {
        if (palavras.indexOf(tipoPokemon.name()) > -1) {
            return true;
        }
    }
    return false;
}

As explained in @Victor Stafusa's answer you do not need to assign a string to each Enum item if the string is equal to the name of the item itself; the name() function already extracts this string for you.

Test:

@Test
public void checkTipo() {
    assertTrue(checkTipo("não dark outro"));
    assertFalse(checkTipo("não darkness outro"));
}
    
16.11.2016 / 16:25