Tuesday, August 20, 2019

String Array Program On Country And City Names

This BlueJ program is on searching city of a country. Two String arrays contains country and city names.



Write a program to read list of countries and their respective cities into arrays. The program will take the name of a country in the list and will display corresponding city name. The program will display error message if the country name is not in the list.


import java.util.*;
class Sentence
{
Scanner sc=new Scanner(System.in);
String country[]=new String[5];
String city[]=new String[5];
String s;
int i;
public void takeDatas()
{
   for(i=0;i<=4;i++)
   {
       
             System.out.print("\nEnter Country:");
             country[i]=sc.nextLine();
             System.out.print("\nEnter Corresponding City:");
             city[i]=sc.nextLine();
   }
  }



 public void show()
 {
      System.out.print("\nEnter Country Name to Search Corresponding City:");
      s=sc.nextLine();
      for(i=0;i<=4;i++)
      {
           if(s.equals(country[i]))
           break;
        }
        if (i==5)
        System.out.print("\nCountry Not Found in List.");
        else
        System.out.print("\nCorresponding City: "+city[i]);
     
  }
      public static void main(String args[])throws Exception
{
     Sentence ob=new Sentence();
     ob.takeDatas();
     ob.show();
}
}


Related Post: BlueJ Programs on String/Sentence

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner