Question: In this assignment, you will be making a program that reads from a file called classes.txt and displaying that information to the user. The goal
In this assignment, you will be making a program that reads from a file called classes.txt and displaying that information to the user. The goal for this assignment is to learn how to handle potential exceptions. Use the given template to implement exception-handling techniques.
import java.util.*; import java.io.*; import java.lang.*;
public class Main { public static void main(String[] args) { Scanner s; Scanner keyboard = new Scanner(System.in);
/* insert code here */ s = new Scanner(("file/classes.txt"));
String line; String[] parts;
int numClasses = Integer.parseInt(s.nextLine()); String[] courseNumbers = new String[numClasses]; int[] creditHours = new int[numClasses]; String[] times = new String[numClasses]; String[] prerequisites = new String[numClasses]; String[] courseNames = new String[numClasses];
for (int i = 0; i < numClasses; i++) { line = s.nextLine(); parts = line.split(";");
courseNumbers[i] = parts[0];
/* insert code here */ creditHours[i] = Integer.parseInt(parts[1]);
/* insert code here */ times[i] = parts[2]; if (times[i].equals("")) throw new NoSuchFieldException(); /* insert code here */ prerequisites[i] = parts[3]; if (prerequisites[i].equals("")) throw new NoSuchFieldException();
/* insert code here */ courseNames[i] = parts[4];
} /*Menu: * P - Print out courses * B - print all course names * C - print all course numbers * Q - Quit */ char input = '?'; do { System.out.println("Choose an option: "); input = (keyboard.nextLine()).toUpperCase().charAt(0); if (input == 'P') printCourses(courseNumbers, creditHours, times, prerequisites, courseNames); else if (input == 'B') printCourseNames(courseNames); else if (input == 'C') printCourseNumbers(courseNumbers); else if (input == 'Q') {
} else System.out.println("Invalid Option"); } while(input != 'Q');
} public static void printCourseNames(String[] names) { System.out.println("COURSE NAMES:"); for (int i = 0; i < names.length; i++) { System.out.println(names[i]); }
System.out.println(); } public static void printCourseNumbers(String[] nums) { System.out.println("COURSE NUMBERS:"); for (int i = 0; i < nums.length; i++) System.out.println(nums[i]);
System.out.println(); }
public static void printCourses(String[] num, int[] hours,String[] times, String[] pre, String[] names) { for (int i = 0; i < num.length; i++) { System.out.print( "Course Number: " + num[i]); System.out.print( " Name: " + names[i]); System.out.print(" Credit Hours: "); if (hours[i] == -1) System.out.print(num[i]+ " does not have any credit hours"); else System.out.print(hours[i]); System.out.print( " Time: " + times[i]); String[] parts = pre[i].split(","); System.out.print(" Prereqs : "); for (int p = 0; p < parts.length; p++) { if (parts.length > 1) { if (p < parts.length - 1) System.out.print(parts[p] + ", "); else System.out.print("and " + parts[p]); } else { System.out.print(parts[p]); } } System.out.println(" "); } } }
how to solve those two errors
error: unreported exception NoSuchFieldException; must be caught or declared to be thrown throw new NoSuchFieldException(); ^
Main.java:46: error: unreported exception NoSuchFieldException; must be caught or declared to be thrown throw new NoSuchFieldException(); ^
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
