简体   繁体   中英

Trying to Generate a string that matches a Regular Expression in Order (From 0 to N size of the String and from 1 to 255 ASCII caracters) in JAVA

The code is taking a value option chooses beetween the 2 Lists that are going to get filled by generating these compatible Strings to the Regular Expressions, there are 2 regular Expressions.

Those regular expressions are going to be compared later. The "ascii" variable is a String with every single caracter in the ASCCI code.

Right now the code seem to not end when I start it, I dont really know where exactly is my mistake here.

static public void ASCIIString(int option){
        String res="";
        int n=0;

cicleG: while(true){ 
            n++;
            int x=n-1;
            int []aux = new int [n];
            for(int i=0;i<n;i++){
                aux[i]=0;
            }

cicle:      while(aux[0]<ascii.length()){
                for(int i=0;i<n;i++){
                    res+=(ascii.charAt(aux[i])+"");
                }
                aux[x]++;
                while(aux[x]==ascii.length() && x>0){
                    aux[x]=0;
                    aux[--x]++;
                }
                x=n-1;
                for (int i = 0; i < Lista1.size(); i++) {
                   if(option==1)
                        if(res.equals(Lista1.get(i))){
                            continue cicle;
                        }
                   else
                        if(res.equals(Lista2.get(i))){
                            continue cicle;
                        }    
                }

                if(option==1)
                      if(res.matches(expression1)){
                          Lista1.add(res);
                          break cicleG;
                      }
                else
                      if(res.matches(expression2)){
                          Lista2.add(res);
                          break cicleG;
                      }

            }
        }
    }

And the code for inicialization is this one:

static ArrayList<String> Lista1,Lista2;
    static String ascii="";
    static String expression1="",expression2="";
    for (int i = 1; i <= 255; i++) {
            ascii+=(char)i;
        }
    Lista1= new ArrayList();
    Lista2= new ArrayList();
    expression1=lenguaje1.getText();
    expression2=lenguaje2.getText(); 

Guessing : one fine reason for your second loop to never stop could be

while(aux[0]<ascii.length()){

That doesn't sound to convincing ...

But honestly, the real answer here ... your problem is of a different nature. The real problem here: you created over complicated and super hard to read/understand code! You have multi level nested loops, with conditions and goto jumps . Don't get me wrong, but you created a huge mess here! It is absolutely no surprise that you don't understand what this code is doing; and as a consequence, it is also no surprise that this code is not doing what you want it to do.

Thus, the real answer here is: throw your code away . Now.

Then write down a clear specification of your requirements (your introducing text is not at all clear); and then start implementing those requirements. But instead of just writing down code, you should really focus on writing code that is easy to read and understand . You see, even if "we" figure the problem with your current code, and you fix it - when you come back in 2 or 4 weeks to enhance that code - you will be lost.

Long story short: the code that you provided took maybe 1 hour to write. It will take easily 2,3 hours to "re-understand" what it is doing in a few weeks from now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM