Question
Reading From a Text File and Handling Exceptions In this assessment, you will design and code a Java console application that reads a text file
Reading From a Text File and Handling Exceptions
In this assessment, you will design and code a Java console application that reads a text file containing course information and prints out this information to the screen. The application will raise an exception if it encounters problems reading the text file.
The requirements of this application are as follows: The application is to read a text file called "courses.txt" that resides in the home folder of the NetBeans project.
The "courses.txt" contains information that describes three courses. Each course is described by three fields separated by spaces on a line by themselves. The three fields that describe a course are:
- Course code
- Course credit hours
- Course title
- The application is to read this text file and prints out its content on the screen for each course. If the application encounters an error, it should raise an exception, prints out an error message, and then exits the program.
- Use these three lines to populate the "course.txt" file:
- IT2249 6 Introduction to Programming with Java
- IT2230 3 Introduction to Database Systems
- IT4789 3 Mobile Cloud Computing Application Development
- Successful completion of this assignment will show the correct content of the "courses.txt" printed out when the application is run. Your program output should look like this sample output:
This is what I have so far but I am stuck
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
public class courses {
private static String filePath;
private static Scanner scan;
public static void main(String[] args) {
filePath = "C:/users/Franky Caba/eclipse-workplace/U2A1_ReadTextFileHandleExcep/src/courses";
//set the string into path object
Path path = Paths.get(filePath);
System.out.println("Teachers Copy ");
//close in a try-catch so that we can cach any type of exception
try {
scan = new Scanner(path);
//read file lien by line
while (scan.hasNextLine()) {
String[] word_tokens = scan.nextLine().split(" ", 3);
//print the array by checking if wordtokens have element in it
if (word_tokens.length > 1) {
System.out.println("Course Code : " + word_tokens[0] + " | Credit hours : " + word_tokens[1]
+ " | Course title : " + word_tokens[2]);
}
}
} catch (IOException ex) {
System.out.println("Unable to Read the file ");
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started