Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: ArrayIndexOutOfBoundsException issue (Beginner?)

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ArrayIndexOutOfBoundsException issue (Beginner?)

    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:
    Jeffrey,Brandon,brand1jd,957030
    Kayla,Neff,neff1km,736165
    Michael,Wilson,wilso2mw,875541

    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:

    Test,student,seeli1p_s,

    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";
    else
    lineitems[3] = "0";
    }
    else
    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
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: ArrayIndexOutOfBoundsException issue (Beginner?)

    Truthfully, there is no good way of doing this; just a couple of different hacks to get it working. Easiest will just be to open the text file and correct the record. You could write a function that goes through the text file and corrects it, but that can be a pain. Another option is to catch those ArrayOutOfIndexExceptions, notify the user there is a problem and continue without that record. If you are writing the records to the text file as part of a basic persistency exercise you should first check for blank data and 'pad' it with zero's or whitespaces so the records are always the same structure.

    This problem you are having illustrates (one of) the shortcomings of text files as a medium for programmatically storing data. Whenever I have a project that requires reading/writing data from a text file I always start with the assumption that the file will always be correctly formatted. If my lecturer/client disagrees with my assumption I use XML. XML is the new industry standard for this kind of problem; it just makes more sense.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayIndexOutOfBoundsException issue (Beginner?)

    I continually get a "ArrayIndexOutOfBoundsException"
    Always test the length of the array before trying to index it.

Similar Threads

  1. Help! ArrayIndexOutOfBoundsException
    By ray3 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 1st, 2011, 06:21 PM
  2. Beginner trying to write Java code, has issue w/ printing result and 2 decimals
    By flpanthers1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2011, 11:11 AM
  3. Issue with beginner program
    By suxen in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2011, 08:55 AM
  4. [SOLVED] ArrayIndexOutOfBoundsException
    By Elementality in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 18th, 2011, 07:39 PM
  5. ArrayIndexOutOfBoundsException
    By NightFire91 in forum Exceptions
    Replies: 1
    Last Post: October 31st, 2010, 06:06 AM