JAVA-Create a 2D array using Java programming

Q. Create a 2-D array that will store the following data: 
COUNTRY  => CITY 
USA => New York, Washington DC, Chicago, Boston 
SOUTH AFRICA => Cape Town, Port Elizabeth, Johannesburg 
ENGLAND => London, Glasgow, Liverpool 
FRANCE => Paris 
INDIA => New Delhi, Mumbai, Kolkata, Chennai 
a. Write a JAVA program to take country as user input and display corresponding cities and vice versa. For wrong input, display "NO SUCH COUNTRY!!!" or "NO SUCH CITY!!!" 
b. Store all the cities in another 1-D array in alphabetical order.

import java.util.Scanner;
class country
{
public static void main(String args[])
{
String a[][]={{"USA","New York"," Washington DC","Chicago", "Boston"},{" SOUTH AFRICA","Cape Town"," Port Elizabeth"," Johannesburg"},{"ENGLAND","London","Glasgow","Liverpool"},{"FRANCE","Paris"," "," "," "},{"INDIA","New Delhi", "Mumbai","Kolkata","Chennai"}};
int i,j,flag=0;
String country;
Scanner sc=new Scanner(System.in);
System.out.println("enter country name: ");
country=sc.nextLine();
for(i=0;i<4;i++)
{
if(country.equalsIgnoreCase(a[i][0])==true)
{
flag=1;
break;
}

if(flag==0)
{
System.out.println("No such country!!");
}
else
for(j=1;j<=4;j++)
{
System.out.println(a[i][j]);
}
}

}
Output:
enter country name: INDIA
New Delhi
Mumbai
Kolkata
Chennai
enter country name: RUSSIA
No such country!!



//if you have any problem, comment below

Comments

  1. store all the cities in another 1 d array in alphabetiacl order??

    ReplyDelete

Post a Comment