1  import java.io.File;
  2  import java.io.FileNotFoundException;
  3  import java.io.PrintWriter;
  4  import java.util.Scanner;
  5  
  6  public class PopulationDensity
  7  {
  8     public static void main(String[] args) throws FileNotFoundException
  9     {
 10        // Open input files
 11        Scanner in1 = new Scanner(new File("worldpop.txt")); 
 12        Scanner in2 = new Scanner(new File("worldarea.txt"));
 13  
 14        // Open output file
 15        PrintWriter out = new PrintWriter("world_pop_density.txt"); 
 16  
 17        // Read lines from each file
 18        while (in1.hasNextLine() && in2.hasNextLine())
 19        {
 20           CountryValue population = new CountryValue(in1.nextLine());
 21           CountryValue area = new CountryValue(in2.nextLine());
 22  
 23           // Compute and print the population density
 24           double density = 0;
 25           if (area.getValue() != 0) // Protect against division by zero
 26           {
 27              density = population.getValue() / area.getValue();
 28           }
 29           out.printf("%-40s%15.2f\n", population.getCountry(), density);
 30        }
 31  
 32        in1.close();
 33        in2.close();
 34        out.close();
 35     }
 36  }