0% found this document useful (0 votes)
11 views

Practical 9 - File Processing

Uploaded by

Lungelo Zulu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Practical 9 - File Processing

Uploaded by

Lungelo Zulu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

UNIVERSITY OF KWAZULU-NATAL

COMP102: Compute Programming


Practical 9: File Processing

Monday, 2 October 2023

Question 1: Text Analysis


One way to determine the difficulty of reading a piece of text is to determine the
average length of words in the text.
Write a program that analyses the text file text.txt. The text file contains a number of
lines of text.
Your program should output:
• the number of lines of text
• the number of words in the file
• the average word length of all the words in the file
Output
There are x lines of text in the file.
There are x words in the file.
The average number length of words is x.

Question 2: Separate Data


The text file separateData.txt contains marks for three students over the course of
the year. The first three lines contain the surnames of the students. The rest of the
file contains lines with the following information: surname of student, a space
character, the student’s mark. These lines with student marks are in no particular
order.
Write a program that reads in this data file and:
• Writes all marks belonging to a particular student to its own file i.e. you output
three files with each file containing the marks of one student
• Calculates and outputs the average mark for each student on the console
The following three text files should be created:
1. Gouws.txt
2. Moodley.txt
3. Kingu.txt

1
Question 3: Email Address Extractor
Write a program that scans a text file for possible e-mail addresses and writes all
email address to an output file.
Addresses look like this:
[email protected]
Notice that the address contains no spaces and has an @ sign followed by at least
one period (.). Programs such as this scan through web pages looking for e-mail
addresses that become the targets of spam. Because of this, many web pages
contain disguised e-mail addresses that can’t easily be automatically extracted.
Your program should find 12 email addresses in the text file emailText.txt.

Question 4: Check Braces


A simple syntax check of java files is to count the number of open and closed
brackets. If the numbers are not the same, then a bracket is missing. Write a
program that reads a .java file specified by the user and counts the number of open
brackets ({) and the number of closed brackets (}). It should then print out the
number of brackets missing. Use the file Athlete.java to test.
The braces in the Althlete.java file match perfectly. So, you should have equal
counts for opening and closing braces. You can then add and remove them from the
file to check if your program works as expected.

Question 5: Header Extraction


Write a program that reads a .java file specified by the user and prints all the method
headers only to a file called headers.txt. Use the file Athlete.java to test.
A method header is for example:
public void getNumber(int i).

Your code should write the following headers to the file headers.txt.

You might also like