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. Given a list of marks ranging form 0 to 100, write a program to compute and print the number of students marks

  2. Write a program for restaurant

  3. Program to read bytes from a file

  4. Program which simulate the dictionary in java

  5. Program sets up a String variable containing a paragraph of text of your choice. Extract the words from the text and sort them into alphabetical order

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

  7. Program to declare, initialize and print a 2D array of integers

  8. An applet program of showing new document using showDocument(URL) method

  9. Program to show the use of If-Else Statement

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

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

  12. Even number ,Prime no. ,Factorial of a number

  13. Program to show insertion of StringBuffer, String objects and other Basic Data Types into a StringBuffer object

  14. Write a class whose objects holds a current value and have a method to add that value, printing the new value

  15. Program to show the comparing of Strings for equality

  16. Program to show the use of Labeled Break Statement

  17. An applet program to display Horizontal Color Bar

  18. An applet program to draw different shapes

  19. Program in CORBA which takes a long sentence from client (at least 7 words), passes to the server, server separates out each word and sends back

  20. Program to show an example of using constructors in a class to initialize its data members

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