Well, here i m trying store the 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;
	 try
	  { 
	   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
		for(int i=0;i<=array.length;i++) 
		 {
		  array[i]=input.nextLine();
		  System.out.println("vertex "+array[i]);
		 }

the error that i get is

returned from absgraph
printing # of vertex 4
array size 4
vertex 
vertex a
vertex b
vertex c
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
	at AbsGraph.readInput(AbsGraph.java:80)
	at DriverProgram.main(DriverProgram.java:12)

 ----jGRASP wedge2: exit code for process is 1.

what fuction should i use?

Recommended Answers

All 8 Replies

The first index of an array is 0, which means the last index of an array is one less than its length. So, knowing that, take a look at the condition in your for loop and see if can find and fix the problem.

try this for statement
for(int i=0;i<array.length;i++)

Well, now, that's what I said, isn't it? Is there something wrong with allowing the OP have a feeling of accomplishment by solving it himself? Do you feel the OP is too dense to figure it out alone given the information provided?

Well thanks for your suggestions Masijade and James. But the problem still exists. i ll paste my code further may be theres an error there.

public void readInput()
   {
	  Scanner input = null;
	 try
	  { 
	   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
		for(int i=0;i<array.length;i++) 
		 {
		  array[i]=input.nextLine();
		  System.out.println("vertex "+array[i]);
		 } 
		 
		// reading edges from the file and creating an array of linked list 

		int numEdge = input.nextInt(); 
		
	   String[] edges = new String[numEdge];  
		System.out.println("# of edges "+edges.length);

the eror i get is the same. Notice one of the vertices missing

----jGRASP exec: java DriverProgram

returned from absgraph
printing # of vertex 4
array size 4
vertex 
vertex a
vertex b
vertex c
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Scanner.java:840)
	at java.util.Scanner.next(Scanner.java:1461)
	at java.util.Scanner.nextInt(Scanner.java:2091)
	at java.util.Scanner.nextInt(Scanner.java:2050)
	at AbsGraph.readInput(AbsGraph.java:86)
	at DriverProgram.main(DriverProgram.java:12)

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

I ll paste the input file too.

4
a
b
c
d
5
a b 5
a c 2
b c 8
b d 1
c d 6
print-mst a
quit

Nope all four are there, but the first one is an empty string.

And that exception comes from the second array where, I can only assume, you are trying to set the elements of a String array with LinkedLists.

Nope all four are there, but the first one is an empty string.

And that exception comes from the second array where, I can only assume, you are trying to set the elements of a String array with LinkedLists.

How do i get rid of the empty string? And you got it right. i am creating an array and then using the array i m trying to link it(array of linked lists). If my approach is incorrect do inform me!!

The nextInt call does not retreive the rest of that line, so call nextLine right after that to read the rest of that first line.

If you want an array linked lists then declare an array of linked lists and not an array strings. A better explanation of what you want and the code for that second array might help to help you.

Okay, I see you've marked this one solved and have started a second thread on the "list" topic. I'll go ahead and lock this one now then.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.