Code /* UseCourse2.java CIS 260 David Klick 2012-01-23 This program is a modification of UseCourse.java, but it capitalizes some fields and appends data to the end of the file instead of erasing the previous contents each time it is run. It also uses the FileWriter class instead of the Formatter class when writing to the file. */ import java.io.*; import java.util.Scanner; public class UseCourse2 { private static FileWriter out; public static void main(String[] args) { openFile("courses.txt"); addCourses(); try { if (out != null) out.close(); } catch (Exception e) { System.out.println("Error closing file."); System.exit(2); } } private static void openFile(String filename) { try { out = new FileWriter(filename, true); } catch (IOException e) { System.err.println("Error opening file."); System.exit(1); } } private static void addCourses() { String dept, sec, room, name; int num; Scanner in = new Scanner(System.in); while (true) { System.out.print("Enter department (QUIT to exit): "); dept = in.next().toUpperCase(); if (dept.equalsIgnoreCase("QUIT")) break; System.out.print("Enter course number: "); num = in.nextInt(); System.out.print("Enter section: "); sec = in.next().toUpperCase(); System.out.print("Enter room number: "); in.nextLine(); // skip previous newline room = in.nextLine().toUpperCase(); System.out.print("Enter course name: "); name = in.nextLine(); Course c = new Course(dept, num, sec, room, name); try { out.write(c.getDepartment() + '|' + c.getCourseNumber() + '|' + c.getSection() + '|' + c.getRoom() + '|' + c.getName() + '\n'); } catch (IOException e) { System.err.println("Error writing to file."); System.exit(3); } } } }