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: java.lang.ArrayIndexOutOfBoundsException

  1. #1
    Junior Member anmaston's Avatar
    Join Date
    Apr 2011
    Location
    Plymouth, UK
    Posts
    10
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question java.lang.ArrayIndexOutOfBoundsException

    Hi, I posted a question on here a couple of days ago and somebody came back with a reply realllly quickly, I can only hope that it will be the same in this case, heres my code in its entirity (I've moved it all together/commented so that its more readible).

    As far as I can tell there are no obvious syntax problems but perhaps I am approaching this all wrong? I'm receiving the error from my compiler stating:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 42
    at callhelper.NewMain.main(NewMain.java:92)
    Java Result: 1

    But what gives?, line 92 is around the area where my first array call is used "prefixes[i]" for the while loop,

    /**
     *
     * @author anmaston
     */
    public class NewMain {
     
        /*HERE I SET UP THE NAMES OF REGIONAL "SERVICE CENTRES"*/
        public static final String SWNAME = "South West England";
        public static final String SENAME = "South East England";
     
     
     
        /*HERE I SET UP THE ARRAYS OF POSTCODE PREFIXES FOR WHICH THE
         * PARTICULAR SERVICE CENTRE WILL COVER
         */
        public static final String[] SW = {"SA", "LD", "HR", "WR", "CF", "NP", "GL",
            "BS", "BA", "SN", "SP", "BH", "TA", "DT",
            "EX", "PL", "TQ", "TR"};
     
        public static final String[] SE = {"CV", "NN", "MK", "SG", "CB", "IP", "CO",
            "OX", "HP", "CM", "RG", "ME", "GU", "RH",
            "CT", "TN", "BN", "SO", "LU", "AL", "SS",
            "SL", "PO", "WD", "EN", "HA", "NW", "N", "E",
            "IG", "EC", "WC", "SW", "SE", "SM", "UB", "TW",
            "KT", "CR", "BR", "DA", "RM"};
     
     
        public static void main(String[] args) {
            /*HERE I CREATE SERVICE CENTRES AND ASSIGN THEIR APPROPRIATE
             * CONSTANT VALUES, I AM AWARE THIS ISNT ELEGANT, COULD I DEFINE
             * CONSTANT OBJECTS? WHEN I TRIED I WASN'T ABLE TO ACCESS THE get/set
             * METHODS (Unsuprisingly, when they were declared as static)
             */
            ServiceCentre SWEST = new ServiceCentre();
            SWEST.setName(SWNAME);
            SWEST.setPostcodePrefixes(SW);
     
            ServiceCentre SEAST = new ServiceCentre();
            SEAST.setName(SENAME);
            SEAST.setPostcodePrefixes(SE);
     
            /*INITIALISE BOOLEAN "found" FOR CONDITIONAL STATEMENT*
             * AND ALSO "i" FOR LOOP CONTROL
             */
            Boolean found = false;
            int i = 0;
            String s = "BS482BH"; // THE POSTCODE STRING, FOR WHICH I NEED TO FIND
                                            // THE APPROPRIATE SERVICE CENTRE (SWEST)
     
            String[] strings = s.split("[0-9]"); //I SPLIT THE POSTCODE BEFORE THE FIRST
                                                          //NUMERIC VALUE, GIVING ME A STRING ARRAY
            String string = strings[0]; //I READ ELEMENT 0 INTO STRING (THIS IS THE ONLY BIT I NEED)
            System.out.println(string); //OUTPUT AS "BS", ALL WELL AND GOOD.
     
            /*MY CONDITIONAL W/ NESTED ITERATION AND CONDITIONAL*/
            /*AS FAR AS I CAN SEE MY SYNTAX IS ALL CORRECT, NOTHING FOR NETBEANS TO*
             * MOAN ABOUT ANYWAY, AND I THINK THE LOGIC IS GOOD, BUT I DONT UNDERSTAND
             * WHY IT WILL NOT WORK, PSEUDOCODE ALONGSIDE
             */
            if (found != true) {       //if the items not found then grab the list of prefixes for the south east
                String[] prefixes = SEAST.getPostcodePrefixes();
                while (prefixes[i] != null) {         //then loop through all of the prefixes until the whole of prefixes[] is looped or
                    if (prefixes[i].equals(string)) { //if a prefix in the array is equal to the string, do something and set found to true.
                        System.out.println("found in southeast");
                        found = true;
                    }
                    i++; //increment the loop control for next iteration,
                }
            } else if (found != true) { //if not already found then:
                i = 0;                  //set loop control back to 0
                String[] prefixes = SWEST.getPostcodePrefixes(); //read the next set of prefixes in
                while (prefixes[i] != null) {               //do the same etc etc.
     
                    if (string.equals(prefixes[i])) {
                        System.out.println("found in southwest");
                        found = true;
                    }
     
                    i++;
              }
            }
        }
    }

    Sorry, I probably didn't need to dump the whole code in here but I thought that it may help for completeness Any hints/(constructive)criticisms or words of wisdom would be greatly appreciated

    I'm certain the problem is here :
    if (found != true) { //if the items not found then grab the list of prefixes for the south east
    String[] prefixes = SEAST.getPostcodePrefixes();
    while (prefixes[i] != null) { //then loop through all of the prefixes until the whole of prefixes[] is looped or
    if (prefixes[i].equals(string)) { //if a prefix in the array is equal to the string, do something and set found to true.
    System.out.println("found in southeast");
    found = true;
    }
    i++; //increment the loop control for next iteration,
    }


    Kind Regards,
    Last edited by anmaston; April 8th, 2011 at 02:56 PM. Reason: removed some code to highlight the problem area more.

  2. #2
    Junior Member anmaston's Avatar
    Join Date
    Apr 2011
    Location
    Plymouth, UK
    Posts
    10
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException

    I thought about what Array Index out of bounds actually means, tinkered with it, got the length and defined a for loop instead of a while.
            if (found != true) {       //if the items not found then grab the list of prefixes for the south east
                String[] prefixes = SWEST.getPostcodePrefixes();
                int lngth = prefixes.length;
                for (i = 0; i < lngth; i++) {         //then loop through all of the prefixes until the whole of prefixes[] is looped or
                 System.out.println(prefixes[i]);
                    if (prefixes[i].equals(string)) { //if a prefix in the array is equal to the string, do something and set found to true.
                        System.out.println("found in southwest");
                        found = true;
                    }
     
                }
                }
    Now, where on earth is that "Solved" button....?

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: java.lang.ArrayIndexOutOfBoundsException

    I'm glad you solved this anmaston
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. java.lang.OutOfMemoryError: Java heap space
    By Nikhat in forum Exceptions
    Replies: 7
    Last Post: April 21st, 2011, 07:31 AM
  2. [SOLVED] ArrayIndexOutOfBoundsException
    By Elementality in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 18th, 2011, 06:39 PM
  3. ArrayIndexOutOfBoundsException
    By NightFire91 in forum Exceptions
    Replies: 1
    Last Post: October 31st, 2010, 06:06 AM
  4. AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    By nasi in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 25th, 2010, 10:37 PM
  5. Replies: 2
    Last Post: November 3rd, 2009, 05:28 AM

Tags for this Thread