I got error of ArrayIndexOutOfBounds Exception in following code.

Please help me to how to solve the error.I also used String tokenizer class.

import java.io.*;
import java.lang.*;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.StringTokenizer;


public class CSV{

public static void main(String[] args) {
        // TODO Auto-generated method stub
        String fileName="C:/Users/JAL/Desktop/dataset.csv";
        try {
            BufferedReader br = new BufferedReader( new FileReader(fileName));
            String strLine = null;
            StringTokenizer st = null;
            int lineNumber = 0, tokenNumber = 0;
            int row = 0;
            int col = 0;
            String [][] numbers = new String[100][100];

            while( (fileName = br.readLine()) != null)
            {
                //lineNumber++;

                //break comma separated line using ","
                st = new StringTokenizer(fileName, ",");

                while(st.hasMoreTokens())
                {
                    //display csv values
                    tokenNumber++;
                    numbers[row][col]=st.nextToken();
                    col++;
                    //System.out.println("Line # " + lineNumber + 
                            //", Token # " + tokenNumber 
                            //+ ", Token : "+ st.nextToken());
                }
                row++;
                //reset token number
                tokenNumber = 0;

            }
        } 
        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

Recommended Answers

All 7 Replies

What is ur program actually doing??? what is present in the csv file??

If I were to guess where you get your index out of bounds error it would be on line 38. Your initial pass through your 2nd while loop will be fine as you have set your col value to zero but once you finish that 2nd while loop, the value does not set back to zero. It will pass your maximum value of 100 if you have more than 100 tokens.

Please help me to how to solve the error.

  1. The error message contains the line number where the error happened; go to that line.
  2. Immediately before that line add a print statement to print the value(s) of the index variable(s)/expressions(s) to see exactly when they are going out of range.
  3. Back-track through your code to see why they are going out of range. Add more print statements to show the values of key variables to help identify the exact source of the error.

wen_cai:

you are right,i got error at line 38.
but i have exactly 100 tokens.but still i got error at this line.
so what to do for that?

poojavb:

in my csv file there is 100 rows containing following data

name,age,sex,pincode,disease

aaa,58,Male,36125,flu
bbb,59,Male,36123,gastric
ccc,61,Female,36124,cancer
ddd,65,Female,36124,flu

ans so on..
so now please tell me what to do for removing an error.

do you know what event throws an ArrayIndexOutOfBoundsException?
let's say you have an array of ten ints:

int[] row = new int[10];

the first index for this (and every other) array is 0, the highest possible(last) one is 9 (10-1)

so, if you call

int a = row[x];

with x being an index < 0, or 9 > x => you'll get an ArrayIndexOutOfBoundsException, because you are trying to access a member of the array 'out of the arrays bounds'

commented: thnkas +1

wen_cai's first post tells you exactly what is wrong with your code. Go back and read it again.

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.