简体   繁体   English

尝试在JAVA中生成按顺序匹配正则表达式的字符串(字符串的大小从0到N,ASCII字符从1到255)

[英]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. 代码采用值选项,通过在正则表达式中生成这些兼容的字符串来填充要填充的2个列表之间的选项,其中有2个正则表达式。

Those regular expressions are going to be compared later. 这些正则表达式将在以后进行比较。 The "ascii" variable is a String with every single caracter in the ASCCI code. “ ascii”变量是ASCCI代码中每个字符都有一个字符串。

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 . 您具有条件和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. 您会看到,即使“我们”发现了您当前代码的问题,并且您已解决该问题-当您在2或4周内回来改进该代码时-您也会迷失方向。

Long story short: the code that you provided took maybe 1 hour to write. 长话短说:您提供的代码可能花费了1个小时来编写。 It will take easily 2,3 hours to "re-understand" what it is doing in a few weeks from now. 从现在开始的几周内,将需要2,3个小时来轻松地“重新了解”它在做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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