Write a program to accept the names of 10 cities in a single dimensional String array and their STD (Subscribers Trunk Dialing) codes in another single dimensional array integer array. Search for a name of a city input by the user in the list.If found, display "Search successful" and print the name of the city along with its STD code, or else display the message "Search Unsuccessful, no such city in the list".

 


THE FOLLOWING PROGRAM SEGMENT SHOWS HOW TO ACCEPT THE NAMES AND STD CODES OF 10 CITIES AND SEARCH FOR THE DESIRED S=CITY TO GET ITS STD CODE ALONG WITH IT.

import java.util.*;

class stdcode

{

    public static void main (String args[])

    {

        final int size = 10;

        Scanner ob = new Scanner (System.in);

        String cities[] = new String[size];// store city name

        String stdCode[] = new String[size];//store STD

         System.out.println("Enter "+size + " cities and their STD");

         for(int i = 0; i < size; i++) // loop to run till l-1 length of array

         {

              System.out.println("Enter the city name ");

              cities[i] = ob.nextLine();

               System.out.println("Enter the city's STD code");

               stdCode[i] = ob.nextLine();

            }

             System.out.println("Enter the city name to search for");

             String city = ob.nextLine();

             int idx;

             for(idx = 0 ; idx < size; idx++)

             {

                 if(city.compareToIgnoreCase(cities[idx]) == 0)

                 {

                     break;

                    }

                    }

                    if(idx < size)

                    {

                         System.out.println("Search successful " );

                          System.out.println("City " +cities[idx]);

                           System.out.println("STD code" +stdCode[idx]);

                        }

                        else

                        {

                             System.out.println("Search unsuccessful, no such city in the list");

                            }

                        }

                    }

The above program is compiled and is without any error.

Hope you guys found it helpful.

Please share it with your friends and comment down in the comment section below.

Follow https://askrisfor.blogspot.com/ for more such updates.

Visit us again.

Thank you!!

Comments

Popular Posts