Wednesday, 11 March 2015

[JAVA] [ICSE 2005 Answer 9] Searching An Array By Linear Search Method

Question:
ICSE 2005 Question 9
Write a program to initialize an array of 5 names and initialize another array with their respective telephone numbers. Search for a name input by the users, in the list. If found, display "Search successful" and print the name along with the telephone number, otherwise display "Search unsuccessful. Name not listed."

Source Code:
import java.util.*;
public class ICSE2005_9
{
    public static void main()throws Exception
    {
        String s[]={"Rohan", "Mohon", "Sohan", "Ram", "Shyam"};
        String tel[]={"9852145236", "5784256321", "85632145687", "9889632145", "7896412356"};
        Scanner read=new Scanner(System.in);
        System.out.print("Enter the name to search: ");
        String check=read.next();
        int n=-1;
        for(int i=0; i<5; i++)
            if(check.equals(s[i]))
                n=i;
        if(n<0)
            System.out.println("Search unsuccessful. Not not listed.");
        else
            System.out.println("Search successful.\nName: "+s[n]+"\nTelephone Number: "+tel[n]);
    }
}


Result 1:
OUTPUT:
Enter the name to search: Rohan
Search successful.
Name: Rohan
Telephone Number: 9852145236
Result 2:
OUTPUT:
Enter the name to search: Tanu
Search unsuccessful. Not not listed.

1 comment:

  1. https://kitsonlinetrainings.com/course/microsoft-azure-administration-online-training
    https://kitsonlinetrainings.com/course/java-online-training

    ReplyDelete