Computer programs need to be able to save and retrieve data
on the hard-disk.
An easy method is to save in a sequential text
file. This file can be opened
and edited with a text editor,
like Notepad or ScEdit.
Data files normally have a single data item
on each line of the file.
The files in this archive (CIA Factbook) are text
files with single data items
on each line. The program below searches
through one of the files
to find a specific country, and prints the Gross
Domestic Product.
(1) Download the .zip archive, upack it, load the program, and run it.
(2) Change the program so that it searches for
China.
Does it have a higher GDP than
Germany?
(3) Change the program so that it inputs the name of the desired country.
(4) Open the text file with a text editor.
Explain why searching for "USA"
will not
function correctly.
(5) Explain the reason for changing the program like this:
BufferedReader file = new BufferedReader(
new FileReader("2001.txt"));
int count = 0; //*****************
while (file.ready())
{
String rank = file.readLine();
String country = file.readLine();
String money = file.readLine();
String year = file.readLine();
String wiggles = file.readLine();
if (country.equals("Germany"))
{
System.out.println(country + "\t" + money);
count
= count + 1; //**************
}
}
if (count == 0)
//*********
{
System.out.println("Not Found"); } //*********
(6) There are lots more text files in the
archive. Find the one that contains
data about telephones. Find out
how many telephones there are in Italy -
do this by changing the
program to :
-
open
the telephone file instead of GDP
-
search
for Italy
(7) Change the program to input the file name
and the country name.
So
running the program looks like this:
What
file do you want to open? 2053.txt
What country do you want to see? France
(8) Add a new country to the telephones file - the
country name is Oz,
and
it has only 1 telepone. Check that the program can find the
country.
(9) Notice that the first 4 lines in each file
describe the contents.
Try to change the
program so that it prints the description at the
beginning, as well as the data for the
particular country.
(10) Write a new program that inputs a file name, and then
prints the
first 10
countries (top 10 list) for that file.
That
means that you should use a FOR loop to count to 10,
instead
of a WHILE loop.
import java.io.*;
/********
* Searches a CIAfactBook text file for a specific country,
* and displays the corresponding data.
* The text file looks like this:
*-----------------------------------------
* Rank
* Country
* GDP
* Date of Information
* ~~~~
* 1
* World
* $ 51,480,000,000,000
* 2004 est.
* ~~~~
* 2
* European Union
* $ 11,050,000,000,000
* 2004 est.
* ~~~~
* 3
* United States
* $ 10,990,000,000,000
* 2004 est.
* ...
*--------------------------------------------
* @author Dave Mulkey
* @version 20050123
*/
public class FindCountry
{
public static void main(String[]
args)
{ new FindCountry(); }
public FindCountry()
{
try
{
BufferedReader file = new BufferedReader(new FileReader("2001.txt"));
while (file.ready())
{
String rank = file.readLine();
String country = file.readLine();
String money = file.readLine();
String year = file.readLine();
String wiggles = file.readLine();
if (country.equals("Germany"))
{
System.out.println(country + "\t" + money);
}
}
}
catch (IOException e)
{
System.out.println(e);
}
}
}