File Input (using a Scanner object) Exercises

  < Previous  Next >
  1. Write Java code to calculate and display the population density (rounded to one decimal point) for each of the countries listed in a data file. You can assume that the data file has a country ID in the first column, the population in the second column and the land area in the third column (with columns separated by a single TAB character, '\t'). For example, the first the first few lines of countryData.tab are:
    # Country_id	Population (2019)	Land Area (km^2)
    Afghanistan	38041754	652860
    Albania	2880917	27400
    Algeria	43053054	2381740
    
    This would produce output like:
    Afghanistan	58.3
    Albania	105.1
    Algeria	18.1
    ...
    
    Click HERE for a hint of the five major steps.