I am trying to store the numbers of a text file into a 2D array..

Here's the text file:
1 2 3
2 3 4
4 5 6

But i get the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at BankerAlg.main(BankerAlg.java:30)

Process completed.

Here's the code i made:

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

public class BankerAlg {
    public static void main(String[] args) {
    int[][] myint = new int[2][2];
    int row, column = 0;
		int i=0, j=0;
		BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\50Cent\\Documents\\JCreator Pro\\MyProjects\\BankerAlg\\src\\test.txt")); //reading files in specified directory
		String line;
		while ((line = in.readLine()) != null) //file reading
		 StringTokenizer token = new StringTokenizer(line);
		 	myint[row][column] = Integer.parseInt(token.nextToken());

	// To read the contents of array..	 
     for (i=0; i<3; i++)
        for (j=0; j<3; j++)
          System.out.print(" ");
		}catch( IOException ioException ) {}

PLEASE HELP ME GUYS!... i am stuck at this part of my program only..

Recommended Answers

All 2 Replies

int[][] myint = new int[2][2];

Your data is 3x3 but your array is only 2x2

I tried changing that too but still its giving the same error, but now ArrayIndexOutOfBoundsException: 3...

the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at BankerAlg.main(BankerAlg.java:30)

Process completed.

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.