Java Tutorial

 
 
 
 

Image

import java.io.*;

class search
{
      String str;
      int key,size,seaArr[];

      public void getdata()
      {
             System.out.print(\"Enter how many data you want to enter : \");
             System.out.flush();
             try{
                 BufferedReader obj=new BufferedReader(new 

InputStreamReader(System.in));
                 str=obj.readLine();
                 size=Integer.parseInt(str);
                 seaArr=new int[size];
                 for(int i=0;i<size;i++)
                   {
                       System.out.print(\"Enter element at \"+(i+1)+\"th 

position  :  \");
                       System.out.flush();
                       str=obj.readLine();
                       seaArr[i]=Integer.parseInt(str);
                   }
                }
            catch(Exception e)  {}
      }

      public int LinSrch()
      {
            System.out.println(\"=====LINEAR SEARCH=====\\n\");
            getdata();
            System.out.print(\"\\nEnter Search Key : \");
            System.out.flush();
            try{
              BufferedReader obj=new BufferedReader(new InputStreamReader

(System.in));
              str=obj.readLine();
              key=Integer.parseInt(str);
                for(int i=0;i<size;i++)
                    {
                          if(seaArr[i]==key)
                               return(i+1);
                     }
                 }
             catch(Exception e) {}
             return(0);
        }    
}

class  linSea
{
    public static void main(String args[]) 
    {
           search o1 = new search();
           int result;
           result=o1.LinSrch();
           if(result==0)
                 System.out.println(\"\\nSearch Not Found\");
            else
                 System.out.println(\"\\nSearch is Located at \"+result+\" 

Position\");
    }
}

    Related Post:
  1. Program to show the use of Nested If-Else Statements

  2. Program to show an example of creating String objects from Character Arrays

  3. An applet program to display text

  4. Program to show an example of using a Static Nested Class

  5. Program which creates an Array of character. Make one function with one argument as a character and that function throw a user defined exception

  6. CORBA based application for addition of two mathematical expressions of the form (ax + by) and displaying the result on client side

  7. Program to show the use of Bitwise Operator (&) AND

  8. Program that will count the number of lines in each file that is specified on the command line

  9. Develop a RMI based application for addition of two complex numbers

  10. Corba program to Write a Echo server and client with UDP server and client

  11. File handling program to count number of characters in a file

  12. Simple Applet which will show life cycle of an Applet

  13. Program using session tracking capability of Servlets, which displays information of current user session by HttpServletRequest object

  14. Program to declare, initialize and print a StringBuffer object

  15. Program to find a factorial of a given number

  16. Program to append a StringBuffer by StringBuffer, String objects and other Basic Data Types

  17. Program which will read a string and rewrite it in the alphabetical order

  18. Program to display reverse $ tree

  19. Corba program to encrypt the text that you want to send on the client side. The server side decrypts it after receiving. Append server time.

  20. Program that enabled applet calculator

 
 
Didn't find what you were looking for? Find more on Program of linear search