In: Computer Science
Java program
problem 1
problem 2
Java code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner s = new
Scanner(System.in);
String countries[] =
{"India","US","UK","Srilanka","Bangladesh"};
String capitals[] =
{"Delhi","Washington","London","Colombo","Dhaka"};
System.out.println("Printing Data
in parallel arrays:");
for(int i=0;i<5;i++)
{
System.out.println("Capital of
"+countries[i]+" is "+capitals[i]);
}
System.out.println("-------------------------------------");
find1(countries,capitals);
}
static boolean find1(String countries[],String
capitals[])
{
String key;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Capital for search:");
key = sc.next();
for(int i=0;i<countries.length;i++)
{
if(key.equals(countries[i]))
{
System.out.println("key matched and its corresponding Capital is
"+capitals[i]);
return true;
}
}
return false;
}
}
Execution Screenshots: