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 an example of extraction of characters from a StringBuffer objects

  2. Program to delete mth element from an input string

  3. Create an interface coversions. Which have the following functions Inchestomillimeters(), hptowatts(),wattstohp(),poundstogram(),gramtopounds()

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

  5. RMI application for getting the final total price of the shopping list with some of the following items and quantities chosen by the user

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

  7. Program for calculating area of a circle on server side,when a client sends request along with given radius and then displaying result on client side

  8. Program to read input from the Standard Input Device

  9. Program to show the use of Nested Loops

  10. Program to show an example of Overriding a Base Class Method

  11. Program to display text

  12. An applet program that concatenates two string entered in TextField

  13. An applet program to display moving banner

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

  15. File Handling- JAVA

  16. Program that accepts a shopping list of items from the command line and stores them in a vector. Also provide facility to perform operation

  17. Program of insertion sort

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

  19. Program to declare, initialize and print a String object

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

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