CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2007
    Posts
    16

    out of array bounds error

    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
    Attached Images Attached Images

  2. #2
    Join Date
    Jul 2007
    Posts
    16

    Re: out of array bounds error

    Quote Originally Posted by Iliketofrolic666 View Post
    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
    Code:
    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)
       {
       	   
       }
    }

  3. #3
    Join Date
    Nov 2009
    Posts
    17

    Re: out of array bounds error

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured