runtime error

import java.io.*;
class q
{

	public static void main()throws IOException
	{
		InputStreamReader isr=new InputStreamReader(System.in);
		BufferedReader br=new BufferedReader(isr);
		int arr[]=new int[5];
		for(int i=0;i<=5;i++)
		{
			do
			{
				arr[i]=Integer.parseInt(br.readLine());
			}
			while(arr[i]!=42);
			
		}
		for(int j=0;j<=5;j++)
		{
			while(arr[j]!=42)
			{
				System.out.println(arr[j]);
			}
		}
	}
}

You are probably getting an ArrayIndexOutOfBoundException. The array you created has a size of 5, and the valid indices on the array are 0, 1, 2, 3 and 4. But in the for loop, you have i ranging from 0 to 5 (both including). When, i equals 5, you try to access arr[5], which is ‘illegal’.

1 Like
//