I just wrote a program for searching an element in an array. It worked fine though. BUT, when I changed the access specifier of variable int i
to public
, error comes (it had no explicit access modifier before, so I thought access was default). So what am I doing wrong? Thanks.
public class Search {
public static void main(String args[]) {
int arr[] = new int[5];
public int i;
int num, flag = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array elements");
for (i = 0; i < arr.length; i++)
arr[i] = sc.nextInt();
System.out.println("Enter the number to be found");
num = sc.nextInt();
for (i = 0; i < arr.length; i++) {
if (num == arr[i]) {
System.out.println("Element Found!");
flag = 1;
break;
}
}
if (flag == 0)
System.out.println("Element not found");
}
}