Directions are to Write an application named GoToFar in which you declare an arr
ID: 3639135 • Letter: D
Question
Directions are to Write an application named GoToFar in which you declare an array of five integers and store five values in the array. Write a try block in which you loop to display each successive element of the array, increasing a subscript by 1 on each pass through the loop. Create a catch block that catches the eventual ArrayIndexOutOfBoundsException and displays the message, "Now you've gone too far."any help would be greatly appreciated going to school online, no instructor help and the book is useless as well.
Explanation / Answer
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class GoToFar{
public static void main(String Args[]){
int[] array = new int[5];
array[0]=0;
array[1]=1;
array[2]=2;
array[3]=3;
array[4]=4;
try{
for(int i=0;i<6;++i){
System.out.println(array[i]);
}
}
catch(ArrayIndexOutOfBoundsException e){
//printed just to inform that we have entered the catch block
System.out.println("Oops, we went to far, better go back to 0!");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.