In this activity we read lines from a text file and process them using a StringTokenizer, writing the results to a second text file.
In addition to the Solution document, the project for this activity contains two classes:
There is also a text file sample3.txt containing details of six imaginary students. This file is in the project folder.
Open University Tutors can download a file of details for the students they are currently tutoring in a format known as Comma Separated Value (csv). This is a text file that simply stores each set of details as a line of text delimited by commas, e.g.
P1111111,Black,Olga,O,Mrs,13 Seaside Street,Bigtown,Barset,BT1 1AA
The real-life application has more items than this but for the purposes of this activity we shall only be concerned with these first nine, so the format of each student record is:
PI,Surname,Forenames,Initials,Title,Addr1,Addr2,Addr3,Postcode
Though it is a simple format, csv has the great advantage of being very flexible. It allows the file to be imported into many different programs, such as spreadsheets and word processors.
In this activity you are asked to complete the task of writing a program which will read a csv file line by line, break it up into its constituent parts with the help of a StringTokenizer, and from these parts assemble an address in the format shown by the following example:
Mrs O Black 13 Seaside Street Bigtown Barset BT1 1AA
This will need to incorporate newline characters in the appropriate places.
We can summarise the algorithm for processing the csv file as follows:
Read the input file line by line until a null line is reached.
For each line:
As long as there are more tokens:
Get the next token and store it in an array.
Construct the address using the stored values from the array.
Write the address to the output file.
Use the File|Open File... option to open the input file sample3.txt from the project folder. You will find it contains details of six fictitious students.
In the class CSVProcessor you will find locations numbered 1 to 5 at which code is missing. Read the instructions there and fill in the missing code.
Run the project. You can find out if your solution is correct by opening the output file sample4.txt in the project folder. This should contain six addresses formatted as specified above.
The new line character is not the same on every platform and so "\n" may not produce the desired effect. Instead you could use
String newline = System.getProperty("line.separator");
Then wherever you would have typed "\n" you put newline instead.