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 read the ballots and count the votes cast for each candidate using an array variable count

  2. Program to calculate the frequency for different values of C starting from 0.01 to 0.1 in steps of 0.01

  3. Program of insertion sort

  4. Program to show an example of Polymorphism

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

  6. Given a list of marks ranging form 0 to 100, write a program to compute and print the number of students marks

  7. Program to show the use of Logical OR Operator (|) in If Statements

  8. Corba program of date operations

  9. Program to show an example of modifying String objects

  10. An applet program to give demo of getDocumentBase() and getCodeBase() methods

  11. Program to show an example of extracting subStrings from a String

  12. Program to read the price of an item in decimal form (like 75.95) and print the output in paise (like 7595 paise).

  13. Program to show an example of extraction of characters from a StringBuffer objects

  14. Program to read input from the Standard Input Device

  15. Program to convert the given temperature in faherenhiet to celsius using the following conversion formula C= F- 32/1.8

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

  17. Program that accepts a shopping list of five items from the command line and stores them in vector

  18. Program to show an example of searching Strings for subStrings

  19. Program to display bank account

  20. Program of linear search

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