Compare enum in java

2

How are you?

I'm doing a small project, but I'm stuck in this part.

Use a stringtokenizer to separate a text file and use a class enum to assign the tokens the value with regular expressions. That's fine, but I need to evaluate when a value is already assigned and give a message on the screen.

public enum Tipo1
    {
        funcion(1), String(2),
        sepgenero(3), entero(4),
        funciongenero(5), funcionartista(6),
        funcionfavorito(7), maximogenero(8),
        maximoartista(9), maximofavorito(10),
        validacion(11),gauss(12),
        salida(13),separtista(14),sepnombre(15),separador(16),salto(17),genero(18),artista(19),nombre(20),album(21), ;

        private int valor1;

        private Tipo1 (int valor1){

            this.valor1 = valor1;
        }


        public int getvalor1() {
            return valor1;


    }

    }

    archivos a=new archivos();
        String s1= a.leertxt("->.rock:.;.Stones;.;.paintitblack|.|.black,.,.");

        StringTokenizer st = new StringTokenizer(s1,"[.]");{
            System.out.println("SEMANTICO");
            while(st.hasMoreTokens()) {

              String jugador= st.nextToken();
              ArrayList<String> tokens = new ArrayList<String>();
              Scanner tokenize = new Scanner(jugador);

              while (tokenize.hasNext()) {
                 tokens.add(tokenize.next());


            System.out.println(tokens);

                Map<Sintactico.Tipo1, String> treeMap = new TreeMap<Sintactico.Tipo1, String>();
                    treeMap.put(Tipo1.funcion, "[->]+");
                    treeMap.put(Tipo1.genero, "[a-zA-Z\s]+[:]+");
                    treeMap.put(Tipo1.artista, "[a-zA-Z\s]+[;]");
                    treeMap.put(Tipo1.nombre, "[a-zA-Z\s]+[|]");
                    treeMap.put(Tipo1.album, "[a-zA-Z\s]+[,]");
                    treeMap.put(Tipo1.sepgenero, "[:]+");
                    treeMap.put(Tipo1.separtista, "[;]+");
                    treeMap.put(Tipo1.sepnombre, "[|]+");
                    treeMap.put(Tipo1.separador, "[,]+");
                    treeMap.put(Tipo1.entero, "[0-9]+");
                    treeMap.put(Tipo1.funciongenero, "[!Genero]+");
                    treeMap.put(Tipo1.funcionartista, "[!Artista]+");
                    treeMap.put(Tipo1.funcionfavorito, "[!Favorito]+");
                    treeMap.put(Tipo1.maximogenero, "[~Genero]+");
                    treeMap.put(Tipo1.maximoartista, "[~Artista]+");
                    treeMap.put(Tipo1.maximofavorito, "[~Favorito]+");
                    treeMap.put(Tipo1.validacion, "[>>+[¶]+]+");
                    treeMap.put(Tipo1.gauss, "[+]+");
                    treeMap.put(Tipo1.salida, "[<-]+");
                    treeMap.put(Tipo1.salto, "[\s]+");
                    Iterator<Tipo1> it = treeMap.keySet().iterator();

                for(Tipo1 t:treeMap.keySet()){
                        String cr=treeMap.get(t);
                        String cr1=treeMap.get(Tipo1.artista);
                        Tipo1 key = it.next();


                        if(jugador.matches(cr)){
                            System.out.println(key);
                            }
            }

And this is the result:

SEMANTICO
[->]
funcion
[rock:]
genero
[;]
separtista
lleva
[Stones;]
artista
[;]
separtista
lleva
[paintitblack|]
nombre
[|]
sepnombre
[black,]
album
[,]
separador

How do I evaluate when more than one separator appears, for example?

I appreciate any advice.

    
asked by ashura1568 07.05.2016 в 02:07
source

1 answer

1

I think you do not have anything clear about things. I go in parts, in my opinion. Using StringTokenizer makes sense to use a mechanism to extract chunks of a String based on a delimiter . For example, if we have a data file in the following way:

//ID;NOMBRE;EMAIL;URL
1000;PEPE;[email protected];www.google.es
1001;DAVID;[email protected];www.marca.es
.....

Each row has a structure of fields delimited by the ";" character. We also know that the first piece is the ID, the second NAME, the third EMAIL and the fourth a URL.

As far as I could understand, you want to define a Map in which you associate a regular expression to validate a field based on an enum that identifies it

There is a class in Java called EnumMap that can help you a lot, I define my enum :

public enum TipoRegex {
  ID(1,"[0-9]{1,9}(\.[0-9]{0,2})?$"),
  NOMBRE(2,"[a-zA-ZñÑ\s]{2,50}"),
  EMAIL(3,"w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"),
  URL(4,"^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \?=.-]*)*\/?$");

  public TipoRegex(int posicion,String regex) {
    this.posicion=posiscion;
    this.regex=regex;
  }

  public int getPosicion() {
    return posicion;
  }

  public String getRegex() {
    return regex;
  }

  private int posicion;
  private String regex;

}

And now I use EnumMap :

EnumMap<TipoRegex,Pattern> regexMap=new EnumMap<TipoRegex,Pattern>(TipoRegex.class);
regexMap.put(TipoRegex.ID,createPattern(TipoRegex.ID));
regexMap.put(TipoRegex.NOMBRE,createPattern(TipoRegex.NOMBRE));
regexMap.put(TipoRegex.EMAIL,createPattern(TipoRegex.EMAIL));
regexMap.put(TipoRegex.URL,createPattern(TipoRegex.URL));

I define the method to create the appropriate Pattern:

private Pattern createPattern(TipoRegex tipo) {
  return Pattern.compile(tipo.getRegex());
}

Finally, it only remains to read the file and apply the validations where appropriate:

String[] datos=getLineaFichero().split(";");
for(int index=0;index<datos.length;index ++) {
  if(index==0) {
    if(!regexMap.get(TipoRegex.ID).matches(datos[index]))( {
      //hacer lo que sea por ser un valor no valido
    }
  }
  if(index==1) {
    if(!regexMap.get(TipoRegex.NOMBRE).matches(datos[index]))( {
      //hacer lo que sea por ser un valor no valido
    }
  }
  if(index==2) {
    if(!regexMap.get(TipoRegex.EMAIL).matches(datos[index]))( {
      //hacer lo que sea por ser un valor no valido
    }
  }
  if(index==3) {
    if(!regexMap.get(TipoRegex.URL).matches(datos[index]))( {
      //hacer lo que sea por ser un valor no valido
    }
  }

}

Adapted to your code. If you do not know the number of fields that come in each line of the file then it makes sense to use StringTokenizer . If you also want to detect the data that is based on the regular expression you will have to be sure that no field fulfills two regular expressions and you will also have to implement a method that applies all the regular expressions until you find the one It corresponds to you.

    
answered by 09.05.2016 в 19:41