Java 终止循环中的堆栈溢出错误?

Java 终止循环中的堆栈溢出错误?,java,regex,loops,overflow,Java,Regex,Loops,Overflow,我最近开始和一群朋友一起玩龙与地下城。我决定尝试制作一个程序,允许我按等级、魔法学校等搜索法术。为此,我获取了一个文本文件,其中按拼写名称的字母顺序列出了每个法术及其信息,并创建了一些正则表达式对其进行排序。我最终得到了它,为每个属性提供了正确的结果。但一旦我把它放入一个循环中,一次获得所有东西,我就会得到一个长长的错误列表,从StackOverflowerr开始。据我所知,当你得到无限循环时,这应该会发生,但我的循环肯定会终止。此外,与简单的for或while循环相比,我可以手动执行更多的循环

我最近开始和一群朋友一起玩龙与地下城。我决定尝试制作一个程序,允许我按等级、魔法学校等搜索法术。为此,我获取了一个文本文件,其中按拼写名称的字母顺序列出了每个法术及其信息,并创建了一些正则表达式对其进行排序。我最终得到了它,为每个属性提供了正确的结果。但一旦我把它放入一个循环中,一次获得所有东西,我就会得到一个长长的错误列表,从StackOverflowerr开始。据我所知,当你得到无限循环时,这应该会发生,但我的循环肯定会终止。此外,与简单的for或while循环相比,我可以手动执行更多的循环(使用循环检查我在每个循环末尾用键盘设置的布尔值)

下面是我正在使用的代码。我没有包括Spell类,因为它只是标准的getter/setter和变量声明。我拥有的学校类型只是八所学校的一个枚举

Map<String, Spell> allSpells = new HashMap<String, Spell>();
    ArrayList<Spell> spellArray = new ArrayList<Spell>();

    int finalLevel;
    int lastMatch = 0;
    int startIndex = 0;
    Matcher match;
    String finalTitle;
    Spell.School finalSchool;
    String finalDescription;
    String fullList;

    
    String titleString = ".+:\\n";                                          //Finds the titles of spells
    Pattern titlePattern = Pattern.compile(titleString);
    String levelString = "\\d\\w+-level";                                   //Finds the level of spells
    Pattern levelPattern = Pattern.compile(levelString);
    String schoolString = "(C|c)onjuration|(A|a)bjuration|(E|e)nchantment|(N|n)ecromancy|(E|e)vocation|(D|d)ivination|(I|i)llusion|(T|t)ransmutation";  //Finds the school of spells
    Pattern schoolPattern = Pattern.compile(schoolString);
    String ritualString = "\\(ritual\\)";                                   //Finds if a spell is a ritual
    Pattern ritualPattern = Pattern.compile(ritualString);
    String descriptionString = "\nCasting Time: (.|\\n)+?(\\n\\n)";         //Finds the description of spells
    Pattern descriptionPattern = Pattern.compile(descriptionString);

    try
    {
        BufferedReader in = new BufferedReader(new FileReader("Spell List.txt"));

        // buffer for storing file contents in memory
        StringBuffer stringBuffer = new StringBuffer("");

        // for reading one line
        String line = null;

        // keep reading till readLine returns null
        while ((line = in.readLine()) != null) 
        {
            // keep appending last line read to buffer
            stringBuffer.append(line + "\n");
        }
        fullList = stringBuffer.toString();     //Convert stringBuffer to a normal String. Used for setting fullList = a substring

        boolean cont = true;
        for(int i = 0; i < 100; i++) //This does not need to be set to 100. This is just a temporary number. Anything over 4 gives me this error, but under 4 I am fine. 
        {
            //Spell Title
            match = titlePattern.matcher(fullList);                             
            match.find();                                                       //Makes match point to the first title found
            finalTitle = match.group().substring(0, match.group().length()-1);  //finalTitle is set to found group, without the newline at the end
            allSpells.put(finalTitle, new Spell());                             //Creates unnamed Spell object tied to the matched title in the allSpells map
            spellArray.add(allSpells.get(finalTitle));                          //Adds the unnamed Spell object to a list. 
                                                                                //To be used for iterating through all Spells to find properties matching criteria


            //Spell Level
            match = levelPattern.matcher(fullList.substring(match.end(), match.end()+50));  //Gives an approximate region in which this could appear
            if(match.find())    //Accounts for cantrips. If no match for a level is found, it is set to 0
            {
                finalLevel = Integer.valueOf(match.group().substring(0, 1));
            }
            else
            {
                finalLevel = 0;
            }
            allSpells.get(finalTitle).setSpellLevel(finalLevel);


            //Spell School
            match = schoolPattern.matcher(fullList);
            match.find();
            finalSchool = Spell.School.valueOf(match.group().substring(0, 1).toUpperCase() + match.group().substring(1, match.group().length())); //Capitalizes matched school
            allSpells.get(finalTitle).setSpellSchool(finalSchool);


            //Ritual?
            match = ritualPattern.matcher(fullList.substring(0, 75));
            if(match.find())
            {
                allSpells.get(finalTitle).setRitual(true);
            }
            else
                allSpells.get(finalTitle).setRitual(false);



            //Spell Description
            match = descriptionPattern.matcher(fullList);
            match.find();
            finalDescription = match.group().substring(1);      //Gets rid of the \n at the beginning of the description
            allSpells.get(finalTitle).setDescription(finalDescription);

            lastMatch = match.end();
            System.out.println(finalTitle);
            fullList = fullList.substring(lastMatch);
            
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
Map allSpells=newhashmap();
ArrayList spellArray=新的ArrayList();
国际最终水平;
int lastMatch=0;
int startIndex=0;
匹配器匹配;
弦乐演奏;
拼写。学校最终学校;
字符串最终描述;
字符串完整列表;
字符串标题字符串=“.+:\\n”//查找咒语的标题
Pattern titlePattern=Pattern.compile(titleString);
String levelString=“\\d\\w+-level”//查找法术等级
Pattern-levelPattern=Pattern.compile(levelString);
String schoolString=“(C | C)onjuration |(A | A)bjuration |(E | E)nchantment |(N | N)ecromance |(E | E)职业|(D | D)文明|(I | I)融合|(T | T)|”//找到魔法学校
Pattern schoolPattern=Pattern.compile(schoolString);
字符串ritualString=“\\(riture\\)”//发现咒语是否是一种仪式
Pattern ritualPattern=Pattern.compile(ritualString);
字符串描述字符串=“\n暂停时间:(.|\\n)+?(\\n\\n)”//查找咒语的描述
Pattern descriptionPattern=Pattern.compile(descriptionString);
尝试
{
BufferedReader in=new BufferedReader(new FileReader(“Spell List.txt”);
//用于在内存中存储文件内容的缓冲区
StringBuffer StringBuffer=新的StringBuffer(“”);
//读一行
字符串行=null;
//继续读取,直到readLine返回null
而((line=in.readLine())!=null)
{
//继续将最后读取的行追加到缓冲区
stringBuffer.append(行+“\n”);
}
fullList=stringBuffer.toString();//将stringBuffer转换为普通字符串。用于设置fullList=a子字符串
布尔控制=真;
for(int i=0;i<100;i++)//这不需要设置为100。这只是一个临时数字。任何大于4的值都会产生此错误,但小于4的值我就可以了。
{
//拼写标题
match=标题模式匹配器(完整列表);
match.find();//使匹配点指向找到的第一个标题
finalTitle=match.group().substring(0,match.group().length()-1);//finalTitle设置为found group,末尾没有换行符
allSpells.put(finalTitle,new Spell());//创建绑定到allSpells地图中匹配标题的未命名拼写对象
spellArray.add(allSpells.get(finalTitle));//将未命名的拼写对象添加到列表中。
//用于迭代所有法术,以查找符合条件的属性
//法术等级
match=levelPattern.matcher(fullList.substring(match.end(),match.end()+50));//给出了可能出现此问题的大致区域
if(match.find())//cantrips的帐户。如果找不到与某个级别匹配的帐户,则将其设置为0
{
finalLevel=Integer.valueOf(match.group().substring(0,1));
}
其他的
{
最终级别=0;
}
所有法术。获取(最终等级)。设置等级(最终等级);
//拼写学校
match=schoolPattern.matcher(完整列表);
match.find();
finalSchool=Spell.School.valueOf(match.group().substring(0,1).toUpperCase()+match.group().substring(1,match.group().length());//将匹配的学校大写
所有咒语。获取(最终目标)。设置学校(最终目标);
//仪式?
match=ritualPattern.matcher(fullList.substring(0,75));
if(match.find())
{
所有法术.get(finalTitle).setriture(true);
}
其他的
所有法术.get(finalTitle).setriture(false);
//拼写描述
match=descriptionPattern.matcher(完整列表);
match.find();
finalDescription=match.group().substring(1);//删除描述开头的\n
allSpells.get(finalTitle).setDescription(finalDescription);
lastMatch=match.end();
系统输出打印LN(finalTitle);
fullList=fullList.substring(lastMatch);
}
}
捕获(例外e)
{
e、 printStackTrace();
}
如果有用的话,我有我正在使用的列表。 正如我在代码的注释中提到的,循环4次以上会出现此错误,但4次以下则不会。我也尝试过在while循环中这样做,但我得到了相同的错误

我曾尝试在网上搜索解决方案,但我看到的关于此错误的所有信息都在谈论再次发生
"(?ms)^Casting Time: (.+?)\n\n"