Display set of names from array


 

Display set of names from array

In this section, you will learn how to display the set of names from array.

In this section, you will learn how to display the set of names from array.

Display set of names from array

In this section, you will learn how to display the set of names from array.

In the given code, we have declared an array of string consisting of some countries name. We have prompted the user to enter a character. If the character matches with starting character of any array element, it will display the set of countries name from the array.

Example:

import java.util.*;

class DisplaySetOfNames{
public static void main(String[] args) 
{
String[] countries = {"Afghanistan", "Albania", "Algeria", "Andorra", "Angola","Argentina"
,"Armenia","Austria","Bahamas","Bahrain", "Bangladesh","Barbados", "Belarus","Belgium",
"Benin","Bhutan","Bolivia","Bosnia & Herzegovina","Botswana","Brazil","Bulgaria",
"Burkina Faso","Burma","Burundi","Cambodia","Cameroon","Canada", "China","Colombia",
"Comoros","Congo","Croatia","Cuba","Cyprus","Czech Republic","Denmark", "Georgia",
"Germany","Ghana","Great Britain","Greece","Hungary","Holland","India","Iran","Iraq",
"Italy","Somalia", "Spain", "Sri Lanka", "Sudan","Suriname", "Swaziland","Sweden",
"Switzerland", "Syria","Uganda","Ukraine","United Arab Emirates","United Kingdom",
"United States","Uruguay","Uzbekistan","Vanuatu","Venezuela","Vietnam",
"Yemen","Zaire","Zambia","Zimbabwe"};
Scanner input=new Scanner(System.in);
System.out.print("Enter letter: ");
String letter=input.next().substring(0,1);
System.out.println();
for(int i=0;i<countries.length;i++){
if(countries[i].startsWith(letter)){
System.out.println(countries[i]);
}
}
}
}

Output:

Enter letter: A

Afghanistan
Albania
Algeria
Andorra
Angola
Argentina
Armenia
Austria

Ads