Programming and Logic 1: Java In this program you\'ll need to store the populati
ID: 3836050 • Letter: P
Question
Programming and Logic 1: Java
In this program you'll need to store the populations of 12 countries.
Define two arrays that will be used in parallel to store the names of the countries and their populations.
Write a loop that uses these arrays to print each country's name and its population.
For an extra 7 points
Add a loop in your main method which asks the user running it if they would like to look up a given country's population. When they indicate “No” the program terminates.
Prompt user: What country would you like to look up?
Upon user entering something the program searches the country names array to see if it's there..
If it is output the country name searched for and it's corresponding population
If it is not output the country name searched for and that it was not found.
China India United States Indonesia Brazil Pakistan Nigeria Bangladesh Russia Japan 1367960000 12626700000 319111000 252164800 203462000 188172000 178517000 157339000 146149200 127090000Explanation / Answer
// Import statements
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//Class begins
public class PopulationOfCountries
{
// Main Method
public static void main(String[] args) throws IOException {
//Population Array
int[] population = new int[12];
//Country Array
String[] countryNames = new String[12];
// Initializing population values
population= new int[]{136796000,126267000,319111000,21346200,18817200,72721212,12642323,36444564,1239445,2436453,1123445,1212344};
//Initializing Country names
countryNames= new String[]{"China","America","United States","Indonesia","Brazil","Nigeria","Bangadesh","Poland","BHARAT","Thailand","Newjersy","Singapore"};
// FOr loop for printing data
for(int i=0;i<population.length;i++){
System.out.println(countryNames[i]+ "--" +population[i] );
}
// For loop asking the requirement of user
for (int i= 1; i<i+1; i++){
System.out.println("Would you like to lookup a countrys population [Yes OR No]");
//reading information from user
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String choice =br.readLine();
//Exit the code if the user send NO
if(choice.equalsIgnoreCase("No")){
System.exit(0);
}
else{
//Printing the population based on the user's required country name
System.out.println("What country would you like to look up?");
String countryName =br.readLine();
for (i = 0; i < 12; i ++)
{
if(countryName.equalsIgnoreCase(countryNames[i]))
{
System.out.println(countryNames[i] + "----" + population[i]);
break;
}
}
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.