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

Thread: Array Index out of bounds Exception

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

    Default Array Index out of bounds Exception

    My code is generating Array Index out of bounds Exception after creating a array of 10 numbers that go from 0 to 99. Any help would be appreciated.

    Thanks

    public class SwapMaxMin {
     
    	public static void main(String[] args) {
     
    		// declare an integer array with 10 elements
    		int [] myScore = new int [10];
    		int i;
     
    		// initialize an array with random numbers
    		for (i = 0; i < myScore.length; i++)
    			myScore[i] = (int)(Math.random()*100);
    		// print an array
    		for (i = 0; i < myScore.length; i++)
    			System.out.println("myScore[" + i + "]= " + myScore[i]);
     
     
     
    		int max = myScore[0];
    		int indexOfMax = 0;
     
    		for ( i = 0; i < myScore.length; i++);
    			if (myScore[i] > max){
    				max = myScore[i];
    				indexOfMax = i;
    				}
     
    		int min = myScore[0];
    		int indexOfMin = 0;
     
    		for( i = 0; i < myScore.length; i++);
    			if (myScore[i] < min) {
    				min = myScore[i];
    				indexOfMin = i;
    			}
    			System.out.println("The largest number is " + max + " and at [ " + indexOfMax + " ] " );
    			System.out.println("The smallest number is " + min + " and at [ " + indexOfMin + " ] " );
    		}
    }


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Array Index out of bounds Exception

    Hello Tohtekcop!
    		for ( i = 0; i < myScore.length; i++);

    You accidentally(?) added semicolons after the for statement in the last two for loops. These may have caused the Exception.

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Array Index out of bounds Exception

    		for ( i = 0; i < myScore.length; i++);
    			if (myScore[i] > max){
    				max = myScore[i];
    				indexOfMax = i;
    				}
     
    		int min = myScore[0];
    		int indexOfMin = 0;
     
    		for( i = 0; i < myScore.length; i++);

    Your for loops end with a semi-colon instead of opening a new curly brace, sort them out and all will be fine.


    Edit: Beaten to it.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  4. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Index out of bounds Exception

    Ugh.... Thanks! How do I change the topic to solved?

  5. #5
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Array Index out of bounds Exception

    Quote Originally Posted by Tohtekcop View Post
    Ugh.... Thanks! How do I change the topic to solved?
    Go to Thread Tools and then Mark this thread as solved.

Similar Threads

  1. ArrayList initial capacity problem (Index out of bounds Exception)
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 11:24 AM
  2. Array exception out of bounds
    By gabberlt in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 17th, 2011, 08:05 AM
  3. Array index out of bounds
    By fortune2k in forum Collections and Generics
    Replies: 1
    Last Post: November 30th, 2010, 07:11 AM
  4. String index out of bounds error 5???
    By stealthmonkey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2010, 07:11 PM
  5. Index Out Of Bounds
    By chronoz13 in forum Collections and Generics
    Replies: 1
    Last Post: December 28th, 2009, 12:19 PM