0% found this document useful (0 votes)
4 views

For Dima

The Java program reads student records from 'marks.txt' and categorizes grades into 'pass.txt' and 'fail.txt' based on a passing threshold of 50. It handles incomplete records and potential errors during file operations and grade parsing. Upon completion, it notifies the user to check the output files for results.

Uploaded by

ttiiaa07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

For Dima

The Java program reads student records from 'marks.txt' and categorizes grades into 'pass.txt' and 'fail.txt' based on a passing threshold of 50. It handles incomplete records and potential errors during file operations and grade parsing. Upon completion, it notifies the user to check the output files for results.

Uploaded by

ttiiaa07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.io.

*;

public class StudentGrades {

public static void main(String[] args) {

// Read marks.txt and create pass.txt and fail.txt

processStudentRecords();

// Read marks.txt and create pass.txt and fail.txt

private static void processStudentRecords() {

try (BufferedReader reader = new BufferedReader(new FileReader("marks.txt"));

BufferedWriter passWriter = new BufferedWriter(new FileWriter("pass.txt"));

BufferedWriter failWriter = new BufferedWriter(new FileWriter("fail.txt"))) {

String studentNumber;

String subject;

int grade;

while ((studentNumber = reader.readLine()) != null) {

subject = reader.readLine(); // Read subject

String gradeStr = reader.readLine(); // Read grade as a string

// Check if grade is null or empty

if (gradeStr == null || gradeStr.isEmpty()) {

System.err.println("Incomplete record found. Skipping.");

continue;

grade = Integer.parseInt(gradeStr);

// Check if grade is above 50

if (grade > 50) {


passWriter.write(subject + " " + grade);

passWriter.newLine();

} else {

failWriter.write(subject + " " + grade);

failWriter.newLine();

System.out.println("Processing completed. Check pass.txt and fail.txt.");

} catch (IOException e) {

System.err.println("Error reading/writing files: " + e.getMessage());

} catch (NumberFormatException e) {

System.err.println("Error parsing grade: " + e.getMessage());

You might also like