Search a number from an array in java

The following java program to search a number from a collection of numbers in an array.

import java.io.*;
class Search
{
int sno,n;
int a[];
BufferedReader br;
void get()throws Exception
{
System.out.println("Enter n");
br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
a=new int[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter "+(i+1)+" Element");
a[i]=Integer.parseInt(br.readLine());

}
}
void pro()throws Exception
{
System.out.println("Enter the numbers to be searched");
sno=Integer.parseInt(br.readLine());
int f=0;
for(int i=0;i<n;i++)
{
if(a[i]==sno)
{
f=1;
break;
}
}
if(f==1)
System.out.println(sno+" is found");
else
System.out.println(sno+" is not found");
}
}
class SearchMain
{
public static void main(String arg[])throws Exception
{
Search obj=new Search();
obj.get();
obj.pro();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *