gpt4 book ai didi

java.lang.ArrayIndexOutOfBoundsException : 49 error - NFA Java 异常

转载 作者:行者123 更新时间:2023-11-30 11:19:00 25 4
gpt4 key购买 nike

我正在编写代码来模拟非确定性有限自动机。该程序要求用户输入 NFA,然后输入字符串以测试它们是否属于 NFA。然而,由于这个错误,程序不断崩溃:

java.lang.ArrayIndexOutOfBoundsException: 49

我已经做了很多尝试来查看是否发生溢出以及发生溢出的位置,但我就是想不通。

我的类(class) NFA:

public static class NFA
{
int numStates;
int alphabetSize;
char[] alphabets;
LinkedList[] transitionTable;
boolean[] isFinal;
NFA()
{
numStates = Integer.parseInt(JOptionPane.showInputDialog("Enter Number of States: "));
alphabetSize = Integer.parseInt(JOptionPane.showInputDialog("Enter Number of Alphabets, including epsilon transition 'e': "));
alphabets = new char[alphabetSize];
alphabets[0] = 'e';
for(int i=1;i<alphabetSize;i++)
{
alphabets[i] = (JOptionPane.showInputDialog("Enter alphabet "+i+": ")).charAt(0);
for(int j=0;j<i;j++)
{
System.out.print(alphabets[j]+" "+alphabets[i]);
if(alphabets[j]==alphabets[i])
{
JOptionPane.showMessageDialog(null,"Alphabet '"+alphabets[i]+"' already exists in the alphabet space! Enter some other alphabet.");
i--;
break;
}
}

}
transitionTable = new LinkedList[numStates];
for(int i=0;i<numStates;i++)
{
transitionTable[i] = new LinkedList();
int ch = Integer.parseInt(JOptionPane.showInputDialog("Do you want to add any transition(s) for state s"+i+"? (Y=1, N=0)"));

while(ch == 1)
{
String data = JOptionPane.showInputDialog("Enter transition for State"+i+" in the format 'alphabet-stateNumber':");
transitionTable[i].add(data);
System.out.println("S"+i+": "+transitionTable[i]);
ch = Integer.parseInt(JOptionPane.showInputDialog("Do you want to add more transitions for state s"+i+"? (Y=1, N=0)"));
}

}
isFinal = new boolean[numStates];
for(int k=0;k<numStates;k++)
{
int ans = Integer.parseInt(JOptionPane.showInputDialog("Is s"+k+" an accept state?(1=Y, 0=N)"));
if(ans == 1)
{
isFinal[k] = true;
}
else isFinal[k] = false;
}
}

private boolean accepts(String stringName) {
if(stringName.equals(""))
{
if(isFinal[0]) return true;
else return false;
}
int startState = 0;
return recursiveAccept(stringName, startState, 0);
}

private boolean recursiveAccept(String stringName, int currState, int currIndex)
{
System.out.println("\t\tcurrIndex:"+currIndex+"\tlength() == "+stringName.length());
if(currIndex == stringName.length())
{
if(isFinal[currState])
return true;
else if(transitionTable[currState].size() == 0)
{
return false;
//String[] s = (String[]) transitionTable[currState].toArray();
}
else
{
for(int i=0;i<transitionTable[currState].size();i++)
{
if((""+transitionTable[currState].get(i)).charAt(0) == 'e')
{
System.out.println("\t\tEntered line 114");
if(recursiveAccept(stringName, (""+transitionTable[currState].get(i)).charAt(2), currIndex))
return true;
}
}
}
return false;
}

if(transitionTable[currState].size()==0) //&& currIndex<(stringName.length()-1))
return false;
else
{
for(int i=0;i<transitionTable[currState].size();i++)
{
System.out.println("\t\tLoop at line 129...linked list element : "+transitionTable[currState].get(i));
if((""+transitionTable[currState].get(i)).charAt(0) == 'e')
{
if(recursiveAccept(stringName, (""+transitionTable[currState].get(i)).charAt(2), currIndex))
return true;
}
else if((""+transitionTable[currState].get(i)).charAt(0) == stringName.charAt(currIndex))
{
if(recursiveAccept(stringName, (""+transitionTable[currState].get(i)).charAt(2), currIndex+1))
return true;
}
}
}
return false;
}
}

还有我的主要功能:

public static void main(String[] args) {
// TODO code application logic here

NFA objectName = new NFA();

int choice = 1;
while (choice == 1 )
{
String stringName = JOptionPane.showInputDialog("Enter String :");
//String stringName = "111";
if(objectName.accepts(stringName))
System.out.print(stringName+" is accepted!");
else System.out.print(stringName+" not accepted!");

choice = Integer.parseInt(JOptionPane.showInputDialog("Enter more?(1=Y, 0=N)"));
}
}

如果您能帮助我们理解问题的原因,我们将不胜感激。另外,我是 Java 的新手,所以我的实现可能不是最好的。

最佳答案

我认为下面的代码有一些问题:

if(recursiveAccept(stringName, (""+transitionTable[currState].get(i)).charAt(2), currIndex))
return true;

您只需取字符串的第三个字符。我假设 currState 可以是 10、11 等。

另外我想提一下下面的代码:

String data = JOptionPane.showInputDialog("Enter transition for State"+i+" in the format 'alphabet-stateNumber':");
transitionTable[i].add(data);

没有检查数据是否正确。

无论如何,一般建议是调试代码以重现错误并找出为什么在索引大于状态数的位置引用了一个自动数组。

关于java.lang.ArrayIndexOutOfBoundsException : 49 error - NFA Java 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23474279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com