4
I have an Enum that contains a list of values. I have a method where I receive 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 any value in the Enum. 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 the part where I create the variable guy, he returns in foreach
and does not check the if
, I need to compare word with enum, someone can help me ?
I don’t understand what your question is. Is the code not working? If not, what goes wrong? What are the types of Pokémon you have on your Enum? If the code does not work, could give an example of text in which it does not work, but should work?
– Victor Stafusa
I’ll edit the question.
– Roknauta
You could join the contents of your list of values into a string, like: "value1;value2;Valor3;value4;value5" and then ask in a loop if a stringvalues.contains(word)
– Reginaldo Rigo
Edited question @Victorstafusa.
– Roknauta