Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Closed Thread
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-30-2008, 12:13 AM   PM User | #1
artemis_f
New to the CF scene

 
Join Date: Aug 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
artemis_f is an unknown quantity at this point
need help with java.lang.ArrayIndexOutOfBoundsException

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.
Code:
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.
Code:
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:
Code:
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.
artemis_f is offline  
Old 09-01-2008, 12:25 AM   PM User | #2
artemis_f
New to the CF scene

 
Join Date: Aug 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
artemis_f is an unknown quantity at this point
For anyone else who ever has this problem: ArrayLists are the way to go
artemis_f is offline  
Old 09-08-2008, 05:56 AM   PM User | #3
jiwatramani
New to the CF scene

 
Join Date: Sep 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
jiwatramani is an unknown quantity at this point
i am getting the same error.
java.lang.ArrayIndexOutOfBoundsException: 1

but the problem is, i dont know how to implement Array List.

more important is, i want to know that why does this error come and why are ArrayList the solution to this kind of error ?

would appreciate early replies.
thank you.
jiwatramani is offline  
Old 09-08-2008, 11:33 AM   PM User | #4
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
Please create a second thread with your problem and a copy of your code so that we may figure it out with you.
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline  
Old 10-04-2008, 10:49 AM   PM User | #5
kingfisher
New to the CF scene

 
Join Date: Oct 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
kingfisher is an unknown quantity at this point
Hi artemis_f,
I'm doing the same sort of thing with reading a text file and processing through to output a DFA. I implemented it using an arraylist as per the advice of this thread, but I'm not exactly sure how to implement my Move method, equivalent of your getTransition method for Arraylists...

Also I have another question, do we need a separate epsilontransition method or will the Move method evaluate it too?

early replies would be appreciated.
kingfisher is offline  
Closed Thread

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:13 PM.


Advertisement
Log in to turn off these ads.