Question

I am parsing a .CSV file, line after line and i want to get the values of the columns. So as example for my .CSV file :

time;columnA;columnB,ColumnC
27-08-2013 14:43:00; this is a text; this too; same here

So what I did is store the content in an bidimensional String array (thanks to split()). My array is made as follow :

array[0][x] = "time".
array[y][x] = "27-08-2013 14:43:00";

they are x different columns, but the name of every column is only stored on line[0][x]. they are y different lines, with the value stored as String in it.

My problem is the following, I want to get the [x] position of the data, but when i try to access the last [x] element of the array. I receive this as error-message

java.lang.ArrayIndexOutOfBoundsException: 17
    at IOControl.ReadCsvFile.getPosVar(ReadCsvFile.java:22)
    at IOControl.ReadCsvFile.<init>(ReadCsvFile.java:121)
    at en.window.Main.main(Main.java:48)

Obviously i'm reading to far, but how?

Here my code :

//Retrieves the x position of the variable var given as parameter.
private int getPosVar(String[][] index, String var)
{
    int x = 0;
    boolean cond = false;
    while((index[0][x] != null) && (cond != true))
    {
        if (index[0][x].contains(var) == true)
        {
            cond = true;
        }
        x++;
    }
    System.out.println("x = " +x+ "  val = " +index[0][x]);
    return(x);
}

I thought it may be because I didn't checked that my x value was smaller than the full string. like this :

x < index[x].length

But in fact I didn't changed anything, and when I give an unknown String var it also goes too far. Why?

Was it helpful?

Solution

Checking the validity of index before using it is also a good idea:

if ( index == null || index.length == 0 ) return -1;

Your while loop should look more like this:

while ( x < index[0].length )
{
    if ( index[0][x] == null )
    {
        x++;
        continue; // skip possible null entries.
    }

    if ( index[0][x].contains(var) )
    {
        System.out.println("x = " + x + ", val = " + index[0][x]);
        return x; // return the position found.
    }
    x++;
}
return -1;

Using a for loop (which I prefer):

for ( int x = 0; x < index[0].length; x++ )
{
    if ( index[0][x] == null )
        continue; // skip possible null entries.

    if ( index[0][x].contains(var) )
    {
        System.out.println("x = " + x + ", val = " + index[0][x]);
        return x; // return the position found.
    }
}

OTHER TIPS

x < index[x].length

The issue is not the length of index[x], but that x is too large. You need to check:

index.length < x

you should check against

x < index[0].length

and it would be good to check

index != null && index.length > 0

before accessing index at all.

After you found a correct result your code also increments "x++" so x is moving one element further. If you now find the last Element or / no Element then this will manage to overflow the array bounds, thus

System.out.println("x = " +x+ "  val = " +index[0][x]);

will throw an error.

I would suggest to change it like this:

private int getPosVar(String[][] index, String var)
{
    int x = 0;
    boolean found = false;

    if(index == null || index.length == 0 || var == null)
        return -1;

    while((x < index[0].length))
    {
        if (index[0][x].contains(var))
        {
            System.out.println("x = " +x+ "  val = " +index[0][x]);
            return(x);
        }
        x++;
    }
    System.out.println("  var = " + var + " not found");
    return -1;
}

You're ignoring array bounds completely. Where do you make sure in your while loop that your x variable isn't too big? Nowhere.

At the end of the while loop you increase the value of x. Afterwards you try to get the value again in the sysout method.

EDIT: Try to put the x++ in an else block.

Becase you are incrementing the value after you do the check. Which leads to the next check being too much. You can solve this simply by adding a brake statement like this.

    public static void main(String[] args) {
    String var = "c";
    String[][] index = {{"a","b","c"},{"a","b","c"}};
    int x = 0;
    boolean cond = false;
    while( (index[0][x] != null) && (cond != true) )
    {
        if (index[0][x].contains(var) == true)
        {
            cond = true;
            break;
        }
        x++;
    }
    System.out.println("x = " +x+ "  val = " +index[0][x]);

Also you should always try to use a forloop instead of a while loop they make it much harder to get errors like these if you check for the length of an array.

You can use your old program

public class TwoDStringArray {

static String[][] index = new String[1][3];

public static void main(String[] args) {

    index[0][0] = "amal";
    index[0][1] = "dev";

    int x = 0;
    boolean cond = false;
    String var = "dev";
    while((index[0][x] != null) && (cond != true))
    {

        if (index[0][x++].equals(var) == true)
        {
            cond = true;
            x--;
        }
    }

    System.out.println("x = " +x+ "  val = " +index[0][x] + "    "+ cond);
}

}

O/P ---->>>

x = 1 val = dev true


but you have to notice one thing when you declare static String[][] index = new String[1][3];

here compiler initialize the 'index' with index[0][0]=null , index[0][1]=null , index[0][2]=null

but there is no index[0][3]

so it will show ArrayIndexOutOfBoundsException

so do one thing if there are 'n' elements in your program then declare 'index' like this

static String[][] index = new String[1][n+1];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top