Question: package student.records.lab; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.io.RandomAccessFile; import java.util.Scanner; public class Driver { private final static

package student.records.lab;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import java.io.RandomAccessFile;

import java.util.Scanner;

public class Driver

{

private final static String INFILE = "resources/student.txt";

public static void main(String[] args) throws IOException

{

Scanner keyboard = new Scanner(System.in);

String firstName;

String lastName;

String yearLevel;

String studentName;

String record;

int menuOption = 0;

do

{

displayMenu();

System.out.println("What menu option would you like?");

menuOption = keyboard.nextInt();

switch (menuOption)

{

case 1:

System.out.print("Enter Student First Name:");

firstName = keyboard.next();

System.out.print("Enter Student Last Name:");

lastName = keyboard.next();

System.out.print("Enter student level:");

yearLevel = keyboard.next();

System.out.println("Appending to existing file...");

studentName = firstName + " " + lastName;

appendToFile(studentName, yearLevel);

break;

case 2:

System.out.print("Enter Student First Name:");

firstName = keyboard.next();

System.out.print("Enter Student Last Name:");

lastName = keyboard.next();

System.out.print("Enter student level:");

yearLevel = keyboard.next();

record = yearLevel + " " + firstName + " " + lastName;

deleteAllRecords(record);

break;

case 3:

countStudents();

break;

case 4:

System.out.println("Exiting.... Good bye!!!");

break;

default:

System.out.println("Invalid Menu Option");

}

} while (menuOption != 4);

}

public static void appendToFile(String studentName, String yearLevel) throws IOException

{

try (FileWriter fw = new FileWriter(INFILE, true);

BufferedWriter bw = new BufferedWriter(fw);

PrintWriter out = new PrintWriter(bw))

{

out.println();

out.print(yearLevel + " ");

out.print(studentName);

}

catch (IOException e)

{

e.printStackTrace();

}

}

public static void deleteAllRecords(String record) throws IOException

{

System.out.println("Deleting from file. Record:" + record);

try (RandomAccessFile file = new RandomAccessFile(INFILE, "rw");)

{

String line;

String remainingRecords = "";

while ((line = file.readLine()) != null)

{

if (line.equals(record))

{

continue;

}

remainingRecords += line + " ";

}

try (BufferedWriter writer = new BufferedWriter(new FileWriter(INFILE));)

{

writer.write(remainingRecords);

}

}

}

public static void countStudents() throws IOException

{

try(BufferedReader br = new BufferedReader(new FileReader(INFILE)))

{

String line;

String [] lines;

int cntFreshman=0;

int cntSophmore=0;

int cntJunior=0;

int cntSenior=0;

while ((line = br.readLine()) != null)

{

lines=line.split(" ");

if(lines[0].equals("F"))

{

cntFreshman++;

}

else if(lines[0].equals("s"))

{

cntSophmore++;

}

else if(lines[0].equals("J"))

{

cntJunior++;

}

else if(lines[0].equals("S"))

{

cntSenior++;

}

System.out.println("Freshmans:"+cntFreshman+" Sophmores:"+cntSophmore+" Juniors:"+cntJunior+" Seniors:"+cntSenior);

}}}

public static void displayMenu()

{

System.out.println("Enter a menu choice:" );

System.out.println(" 1. Add a student to the file. ");

System.out.println("2. Delete the contents of the file.");

System.out.println("3. Print out how many seniors, juniors, sophomores, and freshman there are.");

System.out.println("4. Exit.");

}

}

When I run the program the question are correct but when I input the level it gives me an error. Can someone help me out? I'm using Netbeans and I can not add any more class. I got to get it to work with what I have. The following is what I'm getting.

Enter a menu choice:

1. Add a student to the file. 2. Delete the contents of the file. 3. Print out how many seniors, juniors, sophomores, and freshman there are. 4. Exit. What menu option would you like? 1 Enter Student First Name:m Enter Student Last Name:a Enter student level:s Appending to existing file... Enter a menu choice:

java.io.FileNotFoundException: resources\student.txt (The system cannot find the path specified) 1. Add a student to the file. 2. Delete the contents of the file. at java.io.FileOutputStream.open0(Native Method) 3. Print out how many seniors, juniors, sophomores, and freshman there are. 4. Exit. What menu option would you like? at java.io.FileOutputStream.open(FileOutputStream.java:270) at java.io.FileOutputStream.(FileOutputStream.java:213) at java.io.FileOutputStream.(FileOutputStream.java:133) at java.io.FileWriter.(FileWriter.java:78) at student.records.lab.Driver.appendToFile(Driver.java:91) at student.records.lab.Driver.main(Driver.java:58)

This is the instruction:

File I/O (Input/Output) Lab

Given a file of student records that initially looks like this (supplied in the zip):

Students.txt

F James Wilson

s Nathan Irwin

J Sebastian Rodriguez

S Tom Jordan

*F means freshman, s means sophomore, J means junior, and S means senior.

Implement the methods to append to that file, delete all records of the file, and print out how many seniors, juniors, sophomores, and freshman there are.

In the main method, you will find a loop that prompts the user with 4 choices:

Please enter a choice

Add a student to the file.

Delete the contents of the file.

Print out how many seniors, juniors, sophomores, and freshman there are.

Exit.

Remember that to append to a file, you cannot just use the PrintWriter constructor that accepts a file name. Instead, you have to use a FileWriter as the argument to the PrintWriter constructor.

Appending to a file example:

FileWriter fw = new FileWriter("names.txt", true);

PrintWriter pw = new PrintWriter(fw);

pw.print(collegeLevel); pw.print(firstName);

pw.println(lastName);

pw.close();

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!