Good day to all. Faced a JAVA task and I can’t figure it out. There is such an array:

String[][] arr = { {"Kenya", "Nairobi"}, {"Rwanda", "Kigali"}, {"Gambia", "Banjul"}, {"Ghana", "Accra"}, {"Niger", "Niamey"}, {"Zambia", "Lusaka"} }; 

It is necessary that the program would request the name of the country and display its capital. If this country does not exist, then display the appropriate message.

  • What exactly can you not figure out? - JVic
  • How can I do this ... - Nikita Shevchenko
  • Add your current ideas to the question, otherwise this task looks like an academic one, and it is not customary to completely solve such tasks. - Komdosh

2 answers 2

 public static String getCapital(String[][] arr){ for (String[] countryCapital: arr) { if (countryCapital[0].equals("Rwanda")){ return countryCapital[1]; } } return "no country"; } 

input I think it is clear how to make

     String countryName = getCountryNameFromConsole; for(String country : arr) { if (country.name is countryName) then return countryCapital; } return some errorMessage; 

    I think the algorithm is clear