Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a JAVA application that meets the following requirements: Allow the user

ID: 3686404 • Letter: D

Question

Develop a JAVA application that meets the following requirements: Allow the user to enter a list of countries he/she has visited and display the list in alphabetical order. Have the application count how many countries are on the list. If the user enters the same country twice, the application should include it on the list only once. After displaying the list, allow the user to search for a country on the list. The user should be able to search for a country multiple times. The output should properly indicate whether the target country is on the list or not.

Explanation / Answer

import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

/**
* @author Srinivas Palli
*
*/
public class CounyriesList {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner scanner = null;
       try {

           Set<String> countriesList = new TreeSet<String>();
           scanner = new Scanner(System.in);
           do {
               System.out.print("Enter the country:");
               String country = scanner.next();

               System.out.print("Want to add 1 more(y/n):");
               String choice = scanner.next();
               countriesList.add(country);
               if (choice.equalsIgnoreCase("n"))
                   break;
           } while (true);

           System.out.println("Number of Countries Visited:"
                   + countriesList.size());

           System.out.println("List of countries visited in sorted order:");
           Iterator<String> iterator = countriesList.iterator();
           while (iterator.hasNext())
               System.out.println(iterator.next());

           do {
               System.out.print("Enter the country to Search:");
               String searchCountry = scanner.next();
               iterator = countriesList.iterator();
               boolean flag = false;
               while (iterator.hasNext()) {
                   String country = iterator.next();
                   if (country.equalsIgnoreCase(searchCountry)) {
                       flag = true;
                       break;

                   }
               }
               if (flag)
                   System.out.println("Country Found");
               else
                   System.out.println("Country Not Found");

               System.out.print("Want to Search(y/n):");
               String choice = scanner.next();

               if (choice.equalsIgnoreCase("n"))
                   break;
           } while (true);

       } catch (Exception e) {
           // TODO: handle exception
       } finally {

           scanner.close();
       }

   }

}

OUTPUT:

Enter the country:USA
Want to add 1 more(y/n):Y
Enter the country:AMERICA
Want to add 1 more(y/n):Y
Enter the country:INDIA
Want to add 1 more(y/n):Y
Enter the country:ENGLAND
Want to add 1 more(y/n):Y
Enter the country:AUSTRALIA
Want to add 1 more(y/n):Y
Enter the country:ZIMBAMBE
Want to add 1 more(y/n):Y
Enter the country:USA
Want to add 1 more(y/n):Y
Enter the country:PAKISTAN
Want to add 1 more(y/n):N
Number of Countries Visited:7
List of countries visited in sorted order:
AMERICA
AUSTRALIA
ENGLAND
INDIA
PAKISTAN
USA
ZIMBAMBE
Enter the country to Search:BANGLADESH
Country Not Found
Want to Search(y/n):Y
Enter the country to Search:KENYA
Country Not Found
Want to Search(y/n):Y
Enter the country to Search:USA
Country Found
Want to Search(y/n):INDIA
Enter the country to Search:AFGANISTHAN
Country Not Found
Want to Search(y/n):N