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 2 of 2

Thread: Exception in thread "main" java.lang.IndexOutOfBoundsException:

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

    Default Exception in thread "main" java.lang.IndexOutOfBoundsException:

    import java.io.*;
    import java.util.ArrayList;
    import java.util.*;
    import java.io.IOException;
    import java.lang.*;
    import java.io.RandomAccessFile;


    public class ArrayListClass {



    public static void main(String[] args) {

    ArrayList<Integer> integerArrayList = new ArrayList<Integer>();
    ArrayList<Integer> integerArrayList2 = new ArrayList<Integer>();
    ArrayList<Integer> integerArrayList3 = new ArrayList<Integer>();
    ArrayList<Byte> byteArrayList = new ArrayList<Byte>();
    ArrayList<String> stringArrayList = new ArrayList<String>();

    try {

    RandomAccessFile raf = new RandomAccessFile("C://Users/NetBeansProjects/Java/src/sheet1.xml", "rw");

    for (int pos=0;pos<1000;pos++) {

    raf.seek(pos);
    Byte byte0 = raf.readByte();
    byteArrayList.add(byte0);

    }

    raf.close();

    }

    catch (IOException ex)
    {
    System.out.println(ex.toString());
    }

    for (int i=0;i<byteArrayList.size();i++) {

    Byte byte1 = byteArrayList.get(i);
    Byte byte2 = byteArrayList.get(i+1);
    Byte byte3 = byteArrayList.get(i+2);

    String string1 = String.valueOf(byte1);
    String string2 = String.valueOf(byte2);
    String string3 = String.valueOf(byte3);

    int int1 = Integer.parseInt(string1);
    int int2 = Integer.parseInt(string2);
    int int3 = Integer.parseInt(string3);

    int int4 = int1 + int2 + int3;

    integerArrayList.add(int4);
    integerArrayList.add(0);
    integerArrayList.add(0);

    }

    for (int i=0;i<integerArrayList.size();i++) {

    int int5 = integerArrayList.get(i);
    System.out.println(int5);

    }

    Integer[] integerArray = integerArrayList.toArray(new Integer[integerArrayList.size()]);

    int searchfor = 150;

    int i2;
    boolean foundIt = false;

    for (i2 = 0; i2 < integerArray.length; i2++) {

    if (integerArray[i2] == searchfor)

    {
    integerArrayList2.add(i2);

    }
    }
    for (int i=0;i<integerArrayList2.size();i++) {
    int int6 = integerArrayList2.get(i);
    System.out.println(int6);

    }

    }
    }




    run:
    java.io.EOFException
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 863, Size: 863
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.get(ArrayList.java:382)
    at GetsOffsetsClean.main(GetsOffsetsClean.java:46)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Exception in thread "main" java.lang.IndexOutOfBoundsException:

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 863, Size: 863
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.get(ArrayList.java:382)
    at GetsOffsetsClean.main(GetsOffsetsClean.java:46)
    The index to the get() method called on line 46 was past the end of the ArrayList.
    Remember indexes range from 0 to the list length-1.
    The code should test the value of the index to be sure it does not got past the end of the list.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 5
    Last Post: January 23rd, 2013, 05:29 PM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  4. Replies: 2
    Last Post: March 26th, 2010, 11:22 AM
  5. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM

Tags for this Thread