ICSE Class 10 Computer Applications
Question 28 of 46
String Handling — Question 28
Back to all questions 28
Question Question 15
Write a program to accept the names of 10 cities in a single dimensional string array and their STD (Subscribers Trunk Dialling) codes in another single dimensional 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".
import java.util.Scanner;
public class KboatStdCodes
{
public static void main(String args[]) {
final int SIZE = 10;
Scanner in = new Scanner(System.in);
String cities[] = new String[SIZE];
String stdCodes[] = new String[SIZE];
System.out.println("Enter " + SIZE +
" cities and their STD codes:");
for (int i = 0; i < SIZE; i++) {
System.out.print("Enter City Name: ");
cities[i] = in.nextLine();
System.out.print("Enter its STD Code: ");
stdCodes[i] = in.nextLine();
}
System.out.print("Enter name of city to search: ");
String city = in.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: " + stdCodes[idx]);
}
else {
System.out.println("Search Unsuccessful");
}
}
}Output
