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

Thread: java.lang.ArrayIndexOutOfBoundsException - huh?

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

    Default java.lang.ArrayIndexOutOfBoundsException - huh?

    Hi everyone. I am currently taking a beginners course in java programming at the university of Aarhus. We are now 2 weeks in to the course and our teacher has suggested that we should start some small projects to work on, on our own. So i have decided to try and write a bubblesort algorithm.
    Now i havenīt really gotten very far yet but i am getting a failure-message called

    "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at bubbleSort.main(bubbleSort.java:20)"


    I basically do not understand why i am getting it. I hope that someone can explain it to me. And please keep in mind that you may have to "dumb it down" for me since i am only 2 weeks in to the course. I have already tried to look it up here ArrayIndexOutOfBoundsException (Java 2 Platform SE v1.4.2) and i still don't get it.

    import java.util.Vector;
     
    public class bubbleSort {
     
    	public static void main(String[] args) {
     
    		String[] name={"Thor", "Jensen", "Soeren"};
     
    		Vector v = new Vector();
     
    		int zero=0;
    		int one=1;
     
    		int runTimes = name.length;
     
    		while(runTimes>0){
    			if(name[zero].length() >= name[one].length()){      
    				v.insertElementAt(name[zero], 0);
    				zero++;
    				one++;
    				runTimes--;
    			} else {
    				v.insertElementAt(name[one], 0);
    				zero++;
    				one++;
    				runTimes--;
    			}
    		}
     
    		System.out.println(v);
    	}
     
    }
    Last edited by mrUnknown; February 14th, 2012 at 03:33 PM.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException - huh?

    Your array has 3 elements, so valid indexes are 0, 1 and 2. You index it with 'zero' which you only increment (with 'zero++'). What do you want to happen when zero reaches 3? Try 'instrumenting' your code with lines like:
    System.out.println("zero is " + zero + ", one is " + one + ", runTimes is " + runTimes)
    so that you can see how the variables change in value as the code loops.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException - huh?

    okay, iīll try that .. But since iīve already defined zero as 0 should i not then be able to use as index? or maybe you cannot use variables for this? When zero reaches 3 i want the loop to start over, but i must admit that i havenīt really figured out how to do that yet.

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException - huh?

    Your getting java.lang.ArrayIndexOutOfBoundsException because of your variable one.. you initialize it with a value 1.. so When the condition is satisfied the value of your variable one will be greater than the size of your array..

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. [SOLVED] java.lang.ArrayIndexOutOfBoundsException
    By anmaston in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 12th, 2011, 04:43 PM
  3. [SOLVED] ArrayIndexOutOfBoundsException
    By Elementality in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 18th, 2011, 07:39 PM
  4. ArrayIndexOutOfBoundsException
    By NightFire91 in forum Exceptions
    Replies: 1
    Last Post: October 31st, 2010, 06:06 AM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM