Question: Java Program I keep receiving the following error in my code. Can you please fix this error and test to make sure it does not
Java Program
I keep receiving the following error in my code. Can you please fix this error and test to make sure it does not show a error? I've placed bracets but keep receiving the error "reached end of file while parsing private static void displayStudent()" See below
Error:
sh -c javac -classpath .:target/dependency/* -d . $(find . -type f -name '*.java') ./Main.java:121: error: reached end of file while parsing private static void displayStudent() ^ 1 error exit status 1
------------------------------------------
Code:
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class StudentCourseProgram {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice;
do {
System.out.println("Please choose an option:");
System.out.println("1. Add student");
System.out.println("2. Add course");
System.out.println("3. Display student");
System.out.println("4. Display course");
System.out.println("5. Exit");
choice = input.nextInt();
switch (choice) {
case 1:
addStudent();
break;
case 2:
addCourse();
break;
case 3:
displayStudent();
break;
case 4:
displayCourse();
break;
case 5:
System.out.println("Exiting program...");
break;
default:
System.out.println("Invalid option. Please try again.");
}
} while (choice != 5);
}
private static void addStudent() {
Scanner input = new Scanner(System.in);
System.out.println("Adding a student...");
System.out.print("Enter student ID: ");
int id = input.nextInt();
System.out.print("Enter student first name: ");
String firstName = input.next();
System.out.print("Enter student last name: ");
String lastName = input.next();
System.out.print("Enter student address: ");
String address = input.next();
System.out.print("Enter student city: ");
String city = input.next();
System.out.print("Enter student state: ");
String state = input.next();
System.out.print("Enter student zip code: ");
int zip = input.nextInt();
try {
FileWriter writer = new FileWriter("student.txt", true);
Scanner reader = new Scanner(new File("student.txt"));
while (reader.hasNextLine()) {
String line = reader.nextLine();
String[] parts = line.split(",");
if (Integer.parseInt(parts[0]) == id) {
System.out.println("Error: Student with that ID already exists.");
return;
}
}
writer.write(id + "," + firstName + "," + lastName + "," + address + "," + city + "," + state + "," + zip + " ");
System.out.println("Student added successfully.");
reader.close();
writer.close();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
private static void addCourse() {
Scanner input = new Scanner(System.in);
System.out.println("Adding a course...");
System.out.print("Enter course ID: ");
int id = input.nextInt();
System.out.print("Enter course name: ");
String name = input.next();
System.out.print("Enter course description: ");
String description = input.next();
try {
FileWriter writer = new FileWriter("course.txt", true);
Scanner reader = new Scanner(new File("course.txt"));
while (reader.hasNextLine()) {
String line = reader.nextLine();
String[] parts = line.split(",");
if (Integer.parseInt(parts[0]) == id) {
System.out.println("Error: Course with that ID already exists.");
return;
}
}
writer.write(id + "," + name + "," + description + " ");
System.out.println("Course added successfully.");
reader.close();
writer.close();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
private static void displayStudent()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
