LAB EXERCISE 24: Student Data 2
In this exercise, you will learn how to:
1. Insert the Main Method (Main Entry Point to the Java Program)
2. Import the Library Classes which allow data to be read from an external text file
a. import java.io.File
b. import java.io.FileReader
c. import java.io.BufferedReader
d. import java.io.FileNotFoundException
e. import java.io.IOException
3. Declare Variables
4. Create a Void Method
5. Use a Try-Catch block statement for error file handling and exceptions
6. Create the Object from the Class
7 Call the Object’s Method
Pre-requisite: You must complete Lab Exercise 23 – Student Data 1.
This program reads the data from an external file (studentdata.txt file) and then display the read data results on the console.
In the text file, each line is the student’s data which makes up a single record (row).
The record will contain 6 fields separated by a “tab” delimiter: student ID, student first name, last name, course, total points earned and letter grade
1. Create a folder on your desktop Exercise-24
2. Launch Java EE- Eclipse
Note: You will need to use the Java Perspective Workbench for this exercise
3. Setup your Eclipse Workspace to point to the Exercise-24
a. Select File-> Switch Workspace
b. Browse and select your Exercie-24 folder as your Workspace.
a Java Project and name it as StudentDataPart2
2. Create the first Class that will have the Main Method
a. Name the Class as MainApp
b. Choose the main method to insert into the class
a class called Data.
file from the Exercise23 folder into the Exercise24/ StudentDataPart 2 subfolder
NOTE: The studentdata.txt
should be copied inside the StudentDataPart2 project folder.
C. Requirements for the Data Class:
the following Library Classes which will read the data from an external text
a Void Method - readData() that will execute the code to read the data from an
external file – employee.txt
a. Create instance of the object filename from the File library class & pass the file name “studentdata.txt” as a parameter
File filename = new File("studentdata.txt");
b. Use a Try-Catch
block to perform error handling if cannot read data from the external file
b1. Create an instance of the object "fr" from the FileReader class
Pass the value of the "filename" as a parameter
FileReader fr = new FileReader(filename);
b2. Create an instance of object "br" from the BufferredReader library class
Pass the value of the "fr" object as the parameter
BufferedReader br = new BufferedReader(fr);
b3. Create a local String variable to store the data of each record in the line
b4. Use a While Loop to read each record of the text file until the line reaches the end of file.
i. Store the line containing the student's record into an array
Pass the tab “\t” delimiter as the parameter in the line.split method used in separating the fields in the record
String  readLine = line.split("\t");
ii. Extract the data fields from the
array readLine [ ] and store the data into the variables.
Use the readLine [ ] array trim method to remove any empty spaces after each field
id = readLine.trim();
iii. Display the output on the screen
" + id + "\t");
b5. Close the BufferedReader object
c. In the 1st Catch-block,
add the parameter (FileNotFoundException e)
Insert the code below to display the error handling message if file cannot be found
System.out.println("File not found: " + filename.toString());
d. In the 2nd Catch-block, add the parameter (IOException
Insert the code below to display the error handling message if file can be read
System.out.println("Unable to read file: " + filename.toString());
D. Requirements for the MainApp Class:
comments (documentation)– Program Description, Author and Date
an instance of the object - studentInfo from the Data
Data studentInfo = new Data()
3. Call the object's method- writeData:
1. Save your Java code
2. Compile and run your Java program.
3. Verify there is no syntax, logical or run-time errors.
4. When you run the Java program, the data will
be read from the external file – studentdata.txt
F. Submit your exercise in the Canvas Lab Exercise #24 Drop Box.
1. Submit the screen shot of the Eclipse Workbench
window showing the Console output screen.
You can use Paint (save as JPG) or Word to paste the screenshot.
2. Submit the studentdata.txt data file.
3. Zip up and submit the compressed StudentDataPart2 subfolder that is in the Exercise-24
NOTE: Right click on the subfolder and select Send to “Compress Folder”. The file will have a file extension of .zip.
NOTE: You will need to upload the 3 files above separately.