0

why i am getting arrayindexoutofboundexception

import java.util.Scanner; class Main { public static void main(String args[]) { System.out.println("Program to check if a number is already available in array or not."); Scanner sc=new Scanner(System.in); int i,n; System.out.println("Enter the size of array please:"); n=sc.nextInt(); int arr[]=new int[n]; System.out.println("*************Enter elements in array:****************"); for(i=0;i<n;i++) { System.out.println("Element at index:"+i); arr[i]=sc.nextInt(); } System.out.println("**************You Entered:**************"); { for(i=0;i<n;i++) System.out.println("Entered lement at index:"); System.out.println(arr[i]); } } }

2nd Jul 2022, 10:03 PM
Gurmeet Singh
Gurmeet Singh - avatar
1 Answer
+ 3
Gurmeet Singh import java.util.Scanner; class Main { public static void main(String args[]) { System.out.println("Program to check if a number is already available in array or not."); Scanner sc=new Scanner(System.in); int i,n; System.out.println("Enter the size of array please:"); n=sc.nextInt(); int arr[]=new int[n]; System.out.println("*************Enter elements in array:****************"); for(i=0;i<n;i++) { System.out.println("Element at index:"+i); arr[i]=sc.nextInt(); } System.out.println("**************You Entered:**************"); // remove "{" for(i=0;i<n;i++){//put "{" System.out.println("Entered lement at index:"+i); System.out.println(arr[i]); } } }
2nd Jul 2022, 10:20 PM
Aly Alsayed
Aly Alsayed - avatar