CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2012

    ArrayIndexOutOfBoundsException issue

    Having some issues doing some coding work for a java class of mine. The program is supposed to read an file, split the lines into segments, replaces the lines if blank/empty and then reassemble the line.

    The info in the file looks something like:

    The main issue I've been trying to wrap my head around is the fact that one of the lines in the file looks something like this:


    Which the coding i have set up should recognize lacks a 4th segment and should place a "0" as the missing value in the array, yet it currently refuses to pick up that last value for the array. So I continually get a "ArrayIndexOutOfBoundsException" on that line and only that line of input.

    Any ideas/hints as to how to fix/recode this so that it will recognize that the line missing a number at the end needs a "0" there.

    Code Included Below:

    public static String[] CSVReader (String[] lines)
    for (int i = 0; i < 19 ; i++)
    String[] lineitems = new String [4];
    String[] altitems = new String [4];
    lineitems = lines[i].split(",");
    if (lineitems.length > 4)
    System.out.println("Invalid line:" + lines[i]);
    lines[i] = null;
    else if (lineitems.length < 4)
    if (lineitems[0].isEmpty())
    lineitems[0] = "Missing First";
    else if (lineitems[1].isEmpty())
    lineitems[1] = "Missing Last";
    else if (lineitems[2].isEmpty())
    lineitems[2] = "Missing ID";
    lineitems[3] = "0";
    lineitems[2] = lineitems[2].toLowerCase();
    lineitems[2] = lineitems[2].toLowerCase();
    System.out.println(lineitems[0] + "," + lineitems[1] + "," + lineitems[2] + "," + lineitems[3]);
    lines[i] = lineitems[0] + "," + lineitems[1] + "," + lineitems[2] + "," + lineitems[3];
    return lines;

  2. #2
    Join Date
    May 2006

    Re: ArrayIndexOutOfBoundsException issue

    Read the API docs on the split method.

    You will see it returns an array of sub strings. If the string to split only contains 3 parts the returned array will only have a size of 3 so you can't add 0 to the forth element without resizing the array.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.