Skip to content
Advertisement

Program that stores the names and populations of 12 countries two arrays of the same size

Use data below

Countries: USA, Canada, France, Belgium, Argentina, Luxembourg, Spain, Russia, Brazil, South Africa, Algeria, Ghana

Population in millions: 327, 37, 67, 11, 44, 0.6, 46, 144, 209, 56, 41, 28

Use two arrays that may be used in parallel to store the names of the countries and their populations.

Write a loop that neatly prints each country name and its population.

public static void main(String[] args) 
{
    
    
    // 12 countries and population size
    
    String[] countryName = {"USA", "Canada", "France", "Belgium", "Argentina", "Luxembourg", 
                            "Spain", "Russia", "Brazil", "South Africa", "Algeria", "Ghana"}; //declare the country
    
    
    int[] populationSize = {327, 37, 67, 11, 44, 1, 
                            46, 144, 209, 56, 41, 28}; // declare the population
                            
    // A parallel array are when the position match each other ex usa postion 0 and 327 position 0
    
    for
    (
            int i = 0; i <=11; i++
    )
            System.out.printf("Country: %s, Population in millions: %d n", countryName[i], populationSize [i]);
    
        
    
}

}

If you notice from the instructions Luxembourg is suppose to be 0.6 but I put 1. Every time I try to make this a double I get an error. Currently im using int but it has to be a double. Any advice I appreciate it. I already tried changing it to double [] but I still get an error. Changed the population size and the loop below from int to double did not work. Error in java

Advertisement

Answer

you need to change the populationSize to array of Double and assign double values use correct format specifier for double, i have used %.2f f is for floating point number, which includes double and 2 says two digits after decimal point

public static void main(String[] args)  {


        // 12 countries and population size

        String[] countryName = {"USA", "Canada", "France", "Belgium", "Argentina", "Luxembourg", 
                "Spain", "Russia", "Brazil", "South Africa", "Algeria", "Ghana"}; //declare the country


        Double[] populationSize = {327.0, 37.0, 67.0, 11.0, 44.0, 0.6, 
                46.0, 144.0, 209.0, 56.0, 41.0, 28.0}; // declare the population

        // A parallel array are when the position match each other ex usa postion 0 and 327 position 0

        for (int i = 0; i <=11; i++ ) {
            System.out.printf("Country: %s, Population in millions: %.2f n", countryName[i], populationSize [i]);
        }
    }

Output:

Country: USA, Population in millions: 327.00 
Country: Canada, Population in millions: 37.00 
Country: France, Population in millions: 67.00 
Country: Belgium, Population in millions: 11.00 
Country: Argentina, Population in millions: 44.00 
Country: Luxembourg, Population in millions: 0.60 
Country: Spain, Population in millions: 46.00 
Country: Russia, Population in millions: 144.00 
Country: Brazil, Population in millions: 209.00 
Country: South Africa, Population in millions: 56.00 
Country: Algeria, Population in millions: 41.00 
Country: Ghana, Population in millions: 28.00 

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement