Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2012
    Posts
    20
    Rep Power
    0

    Default ArrayIndexOutOfBounds Help

    I have to use a try block to loop through each item in the array and increase a subscript by 1. Then I have to use a catch block that catches the ArrayIndexOutOfBoundsException and displays a message.

    I cant seem to get it to throw the message: Now Youve Gone Too Far. How do I go about creating this message as it tries to display a 6th number that is not in the array?

    PHP Code:
    //GoTooFar.java by Tyler 11/11/2012
    
    import java.util.*;
    
    public class GoTooFar
    {
    	public static void main(String[] args) 
    	{
    	Date today = new Date();
    	try
    	{
    		
    		//Display intro window
    		System.out.println("GoTooFar" + "\nby Tyler " + today);
    		
    		//Initialize arrays
    		int[] numbers = {10 , 11 , 12 , 13 , 14};
    
    		//Display each number in array
    		for (int i = 0; i < numbers.length; ++i)
    		{
    			System.out.print(numbers[i] + "\n");
    		}
    	}
    	catch(ArrayIndexOutOfBoundsException exception)
    	{	
    		System.out.println("Now Youve gone too far");
    		System.exit(0);
    	}
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: ArrayIndexOutOfBounds Help

    Well, your loop doesn't try to go too far.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Join Date
    Oct 2012
    Posts
    20
    Rep Power
    0

    Default Re: ArrayIndexOutOfBounds Help

    Quote Originally Posted by DarrylBurke View Post
    Well, your loop doesn't try to go too far.

    db
    Ive tried this:

    PHP Code:
    for (int i = 0; i < numbers; ++i)
    And it will only display whats in the array. How do I make it try to display a 6th array element without adding a 6th array element?

  4. #4
    Jdsfighter is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Re: ArrayIndexOutOfBounds Help

    Java Code:
            for (int i = 0; i <= numbers.length; ++i)
            {
                System.out.print(numbers[i] + "\n");
            }

  5. #5
    Join Date
    Oct 2012
    Posts
    20
    Rep Power
    0

    Default Re: ArrayIndexOutOfBounds Help

    Quote Originally Posted by Jdsfighter View Post
    Java Code:
            for (int i = 0; i <= numbers.length; ++i)
            {
                System.out.print(numbers[i] + "\n");
            }
    Thank you.

Similar Threads

  1. Replies: 3
    Last Post: 04-09-2012, 05:30 PM
  2. problem with ArrayIndexoutofbounds exception
    By hyma19 in forum New To Java
    Replies: 1
    Last Post: 01-16-2012, 11:13 AM
  3. ArrayIndexOutOfBounds, JTable.getValueAt
    By ZackO in forum AWT / Swing
    Replies: 10
    Last Post: 10-17-2010, 08:29 PM
  4. ArrayIndexOutofBounds Exception
    By atul.goldenstring in forum New To Java
    Replies: 10
    Last Post: 04-10-2010, 11:47 AM
  5. ArrayIndexOutOfBounds
    By SwEeTAcTioN in forum New To Java
    Replies: 6
    Last Post: 12-07-2009, 01:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •