Java Exercise: Analyse Immigrant Data

Your Challenge ...

Further down this page you can find immigration data for the top ten immigrant destination countries, borrowed from Wikipedia. Initialise a 2D array in Java with this data. Then, making use of loops where convenient to avoid unnecessary typing, get your program to answer the following questions or perform the following tasks.

  1. Display the table data (not the headings) on the console, using tabs and/or System.out.printf() to align columns.
  2. What is the total number of immigrants for these countries?
  3. What total percentage of the world's immigrants do these countries have?
  4. Which countries have the highest and lowest percentage of immigrants?
  5. Based on these figures, estimate the total population of all ten countries.

Bonus Challenges for upcoming Java gurus (requires detailed knowledge of array sorting and file parsing):

  1. Read all the data from a text file instead of hardcoding it.
  2. Output this data, sorted by number of immigrants as a percentage of population, highest first.

Some hints ...

Don't be afraid to Google for anything you don't know.

Absolutely avoid repeating anything in your program that you could use a loop for instead.

Use casting where necessary to transform your array objects into the types you need. For example, int value = (int)data[0][1]; . If you continue to study Java, you'll discover better ways of dealing with this situation later on, avoiding error-prone casts. If you already know these ways, feel free to use them.

Start with adding one line of data to a 1D array of Object. Check you can access all the elements, and that you can add numbers from the array together. Then try a 2D array with two rows of data, before moving onto the full thing.

Formatting so much data into a 2D array is tedious; make sure you use copy and paste as much as possible and have some kind of system to formatting the array. If you continue to study Java, eventually you'll know how to read data from files and won't need to do this kind of thing. Meanwhile, it's good for the soul, as my former boss used to say.

You may get stuck. This is normal; the longer you can spend thinking about the problem, trying out possible solutions and Googling for answers without getting demotivated, rather than just looking at the solution, the better! Wrestling with coding problems hugely builds your understanding of programming.

Immigration Data

Taken from


Country Number of Immigrants Percentage of Total Immigrants in the World Immigrants as Percentage of National Population
United States 45,785,090 19.8 14.3
Russia 11,048,064 4.8 7.7
Germany 9,845,244 4.3 11.9
Saudi Arabia 9,060,433 3.9 31.4
United Arab Emirates7,826,981 3.4 83.7
United Kingdom 7,824,131 3.4 12.4
France 7,439,086 3.2 11.6
Canada 7,284,069 3.1 20.7
Australia 6,468,640 2.8 27.7
Spain 6,466,605 2.8 13.8

Example Session

Note: I've replaced some of the data with ???. The headings are optional.


               Country   Immigrants     % world total   % population
         United States     45785090           19.80        14.30
                Russia     11048064            4.80         7.70
               Germany      9845244            4.30        11.90
          Saudi Arabia      9060433            3.90        31.40
  United Arab Emirates      7826981            3.40        83.70
        United Kingdom      7824131            3.40        12.40
                France      7439086            3.20        11.60
                Canada      7284069            3.10        20.70
             Australia      6468640            2.80        27.70
                 Spain      6466605            2.80        13.80

Total immigrants: 119048343
Total percentage of world's immigrants: ???
Country with least immigration: ???
Country with greatest immigration: ???
Total estimated population of all countries: ???


Concepts You'll Need for This Exercise

  • The contents of the first 16 or so videos from my free Java course free Java course, up to, but not including, classes.
  • Google for something like "Java align strings" if you want to use printf to align your table columns but don't know how.