A09

Due Date: Wednesday, 7 April 2021
File(s) to be submitted: CalculateCountryData.java
Sample Output: SampleOutput.html
Starter Files:


SUBMIT   /   Check

Country data (File IO and Error Handling)

Summary

Write a program that reads country data from a file and prints a report on the countries found. The file includes countries' names, populations and areas. The report includes all that and also the countries' population densities.

Your program must handle various error conditions:

Whenever your program skips data, it reports what the problem was, and includes the name of the country that it was reading data for.

Your report doesn't have to be formatted like mine, but if you're planning on going on in CS it really would be a good idea to learn how formatted printing works.

I have created a brief introduction to printf that explains all you need to know in order to lay out the report the way it is in my sample output.

Details

I have provided a class called Country that holds the data for a country. The client must include the country's name, population and area. Do not modify that class. It is exactly as I want it.

I have provided you with two data files. One has "clean" data (no errors), and the other has (lots of) bad data. Do not modify these files. They are exactly as I want them.

Remember that data files go in the project folder (e.g. A09).

Your job is to write the program (CalculateCountryData). It follows the following steps:

  1. Print an introduction. This includes a title for the program, a brief description of what it does, and your name and A-number.
  2. Get the file name from the user. It gives the user three chances to enter a valid file name. It returns a Scanner opened to that file, if successful, or ends the program with error code 1 if not.
  3. Read data from the file. It opens the file and continues reading data until the file has no more data.

    The data consists of

    1. The name of the country (which may include spaces) on a line by itself.
    2. The population of the country (an integer value).
    3. The area of the country (a double value).
    There is an end-of-line after the country's area (so the next country's name starts on the following line).
  4. Write a report on the countries. It prints a table with each country's name, population, area, and population density. (You can get each of those pieces of information from the Country object itself.

Make good use of methods! Have the file-reading method return a List of Countrys. Have the report-printing method accept that List of Countrys. The report-printing method can use the for-each-style loop to go thru the list of countries.

Things that can go wrong are:

Whenever something goes wrong in the reading process, the program reports what the problem was, and includes the name of the country that the program was reading data for when the error happened.

If NetBeans tells you that the Country's name might not have been initialized, you need to: Remember, whenever you use a try block, the program may jump out of that block from any location, so Java can't assume that any part of that code has completed successfully!

Notice that everything that can go wrong (other than the file-not-found exception) only happens in the file-reading method. The method for reading the file name deals with the FNFE. Your other methods shouldn't need to deal with exceptions at all.

Notice also that each exception (again, other than the file-not-found exception) results in a country not being added to the list. What's happening in the file-reading method is:

While the file still has data, you try to read a name, population and area, create a country to represent that area, then add that country to the list of countries you're building.
Of course if anything goes wrong, then the country doesn't get added to the list.
SUBMIT   /   Check