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?
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);
}
}
}
Re: ArrayIndexOutOfBounds Help
Well, your loop doesn't try to go too far.
db
Re: ArrayIndexOutOfBounds Help
Quote:
Originally Posted by
DarrylBurke
Well, your loop doesn't try to go too far.
db
Ive tried this:
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?
Re: ArrayIndexOutOfBounds Help
Code:
for (int i = 0; i <= numbers.length; ++i)
{
System.out.print(numbers[i] + "\n");
}
Re: ArrayIndexOutOfBounds Help
Quote:
Originally Posted by
Jdsfighter
Code:
for (int i = 0; i <= numbers.length; ++i)
{
System.out.print(numbers[i] + "\n");
}
Thank you.