Click to See Complete Forum and Search --> : out of array bounds error


Iliketofrolic666
March 7th, 2010, 07:57 PM
See attached:

the print ln show that the array should be 4 by 3, but
it give an error even when I am trying to set a value at [0][1]

the source code shown is from FSA.java

Iliketofrolic666
March 7th, 2010, 07:58 PM
See attached:

the print ln show that the array should be 4 by 3, but
it give an error even when I am trying to set a value at [0][1]

the source code shown is from FSA.java


import java.io.*;
import java.util.*;

public class FSA{
private String machineName; //name of machine
private int numStates; //number of states
private String alphabet; //a list of distinct symbols
private int[][] transitions; //numStates-by-length_of_alphabet
private String tape; //a string over the alphabet
private int start; //start-state
private boolean[] acceptStates; //set of accept-states


//Use a Scanner to open the file whose name is fileName.
//Input the FSA from this file
public FSA(String fileName) throws IOException
{
FileReader freader = new FileReader(fileName);

Scanner scanner = new Scanner(freader);


//Read in file header
machineName = scanner.nextLine();
numStates = scanner.nextInt();
scanner.nextLine();
start = scanner.nextInt();
scanner.nextLine();
alphabet = scanner.nextLine();

transitions = new int[numStates][alphabet.length()];

System.out.println(alphabet.length()+" "+numStates);
int i,j,k;
do{
i = scanner.nextInt();//state
j = scanner.nextInt();//transition
k = scanner.nextInt();//new state
scanner.nextLine();
if (-1 != i )
{
System.out.println(" i: "+i+" alphabet.charAt(): "+alphabet.charAt(j)+" j: "+j+" K: "+k);
transitions[i][alphabet.charAt(j)] = k;
}

} while (-1 != i);


acceptStates = new boolean[numStates];
for (int temp = 0; temp < numStates; temp++)
{
acceptStates[temp] = false;
}
while (scanner.hasNextLine())
{
String str = scanner.nextLine();
if (-1 != str.indexOf(-1)){break;}
for (int temp = 0; temp < numStates; temp++)
{
if (-1 != str.indexOf(temp))
{
acceptStates[temp] = true;
}
}
}

}


//Display the FSM as show on the next page
public void displayMachine()
{
System.out.println("Machine Name: "+machineName);
System.out.print("Set of States: {");
/*for (int i = 0; i < numStates; i++)
{
System.out.print(i,
}*/
}

//Return the machineName to caller
public String name()
{
return machineName;
}

//Return number of states to caller
public int numberOfStates()
{
return numStates;
}

//Return alphabet back to caller
public String alphabet()
{
return alphabet;
}

//Return start state back to caller
public int start()
{
return start;
}

//Return next state if in current state and see symbol
public int nextState(int current, char symbol)
{
return transitions[current][alphabet.indexOf(symbol)];
}

//Return true or false if k is or is or is not a final state.
public boolean finalState(int k)
{
return acceptStates[k];
}

//Process tape, display transitions and determine if accept or reject tape
public void processTape(String tape)
{

}
}

DavidFongs
March 7th, 2010, 09:33 PM
If you look at the stack trace, you will see you are out of bounds because you aren't trying to set it at [0][1], you are trying to set it at [0][49]

This is your problem:

transitions[i][alphabet.charAt(j)] = k;

alphabet.charAt(j) returns a character, not an integer. You are using the unicode value of alphabet.charAt(j) which is causing the error