Hello all, I am new to this forum. I think this will be an excellent place for learning things and I am looking forward to gaining more java knowledge.
My current predicament is the ArrayIndexOutOfBoundsException I am getting on a program I have written. I haven't been able to figure out why and am hoping someone could point me in the right direction. A warning: the following code probably will have a lot of java bad practice or there might be easier ways to do some things...if you are inclined constructive criticism on that will be appreciated but my main priority is figuring out how to get rid of the error.
I don't know how little or much of the code to include so I am going to show it all to you -
1. I created a WordReader class which has methods that will read various things from a textfile that will be given as a parameter to the method.
package thisPacket;
/*
* WordReader.java - A program to read words from a file
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.*;
public class WordReader {
String[][] transitions;
public String[][] readTransitions(String fileName){
BufferedReader br = null;
String word;
String[] rowOfTransition = new String [3];
String[][] transArray = new String[5][3];
System.out.println("the transitions");
try {
br = new BufferedReader( new FileReader( fileName ) );
br.readLine(); //skip line 1
br.readLine(); //skip line 2
br.readLine(); //skip line 3
br.readLine(); //skip line 4
//transitions begin from fifth line
int index = 0;
// loop and read a line from the file as long as we don't get null
while( ( word = br.readLine() ) != null ){
// add the read word to the wordList
rowOfTransition = word.split(", ");
transArray[index]=rowOfTransition;
System.out.println(index+" "+transArray[index][0]+" "+transArray[index][1]+" "+transArray[index][2]);
index++;
}
} catch (IOException e) {
e.printStackTrace();}
finally {
try {
br.close();}
catch (IOException e) {
e.printStackTrace();}
}
transitions = transArray;
return transitions;
}
}
Here is an example of a valid input text file:
0, 1
q0, q1, q2
q0
q2
q0, 0, q0
q0, 0, q1
q0, 1, q0
q1, 0, none
q1, 1, q2
Now I know that the error is occuring on the readTransitions function on the following line:
transArray[index]=rowOfTransition;
Perhaps the problem is the way I have defined transArray as
String[][] transArray = new String[5][3];
? I know that the number of columns are always going to be 3 but the number of rows can be any number and since in my test file I knew there were only 5 lines to read I put five in there thinking I'll find a way of specifying number of rows later (read: I don't know how to read the number of lines left in the text file from the 5th line to the last line yet).
In case it is relevant here is the rest of my code that calls the readTransitions function
2. The NFAtoDFA class at the moment has a method called GetTransitions which wants to find out on a certain input state (always in first column) and certain input symbol (always in second column) what is the state that gets transitioned to.
package thisPacket;
import java.io.BufferedReader;
import java.util.Iterator;
import java.util.Set;
import java.util.Stack;
public class NFAtoDFA {
WordReader wr = new WordReader();
//! Returns all transitions from this state on specific input
public String[] getTransition(String symbol, String inputState)
{
String[] transTo = null;
int index=0;
String[][] trans = wr.readTransitions("eg of a valid input nfa.txt");
for (int i=0; i==trans.length; i++){
if ((trans[i][0]==inputState)&&(trans[i][1] == symbol)) {
transTo[index] = trans[i][2];
index++;
}
}
for (int i =0; i==transTo.length; i++)
System.out.println("transTo");
System.out.println(transTo);
return transTo;
}
}
And finally 3. Runnable class calls the getTransition method:
package thisPacket;
import java.io.BufferedReader;
public class Runnable {
/**
* @param args
*/
public static void main(String[] args) {
WordReader wr = new WordReader();
NFAtoDFA n2d = new NFAtoDFA();
BufferedReader br;
n2d.getTransition("0","q0");
}
}
The error I get on running Runnable is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at thisPacket.WordReader.readTransitions(WordReader.java:140)
at thisPacket.NFAtoDFA.getTransition(NFAtoDFA.java:38)
at thisPacket.Runnable.main(Runnable.java:15)
I have tried changing the transArray to [10][10] and whatever else and that doesn't work. I probably am not using 2-d arrays correctly. Any insight into how I can solve my problem would be very much appreciated. Thank you.