Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.
I m stuck here. I m trying to store some vertices(4) in an array. and trying to read them. But i m getting an error of "Array Index out of Bounds Exception.
I know the error is in the line i have highlighted(red). And i have tried various methods. But not working.
public void readInput()
Scanner input = null;
input = new Scanner(new FileInputStream("Test1.txt"));
int vertex = input.nextInt();
System.out.println("printing # of vertex "+vertex);
String array = new String[vertex];
System.out.println("array size "+ array.length);
// reading vertex from file and storing them in the array
the error that i get is
----jGRASP exec: java DriverProgram
returned from absgraph
printing # of vertex 4
array size 4
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
This post has been edited by kool001: 02 December 2009 - 11:44 PM
Your for loop should go while < array.length, not <= array.length. Array.length is the number of elements in the array which starts counting at 1, but the indices of the array start at 0, so your last index is array.length - 1;