Question
Write a program to accept the names of 10 cities in a single dimension string array and their STD (Subscribers Trunk Dialing) codes in another single dimension 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’.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class program9
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String[] city = new String[10];
int[] std = new int[10];
int i=0;
System.out.print("Enter city: ");
for ( i = 0; i < 10; i++)
{
city[i] =sc.next();
}
System.out.print("Enter STD: ");
for (i = 0; i < 10; i++)
{
std[i] =sc.nextInt();
}
System.out.print("Enter city name to search: ");
String target = sc.next();
boolean flag = false;
for (i = 0; i < 10; i++)
{
if (city[i].equals(target)==true)
{
System.out.println("Search successful");
System.out.println("City : " + city[i]);
System.out.println("STD code : " + std[i]);
flag = true;
break;
}
}
if(flag==false)
{
System.out.println("Search Unsuccessful, No such city in the list");
}
}
}