import java.util.Scanner; import java.io.FileInputStream; import java.io.FileOutputStream; import java.text.DecimalFormat; import java.io.IOException; public class LabProgram {     public static void main(String[] args) throws IOException {         Scanner scnr = new Scanner(System.in);         // Initialize variables for calculating exam averages         int count = 0;         double sumMid1 = 0, sumMid2 = 0, sumFinal = 0;         // Initialize FileOutputStream for output         FileOutputStream fos = new FileOutputStream("report.txt");         // Prompt for the filename         System.out.print("Enter the name of the TSV file: ");         String fileName = scnr.nextLine();         FileInputStream fis = null;         try {             fis = new FileInputStream(fileName);             // Read the file and build the content string             StringBuilder sb = new StringBuilder();             int ch;             while ((ch = fis.read()) != -1) {                 sb.append((char) ch);             }             // Split the content by lines             String[] lines = sb.toString().split("\n");             // Process each line             for (String line : lines) {                 // Make sure the line contains sufficient data                 if (line.isEmpty()) {                     continue;                 }                 String[] tokens = line.trim().split("\t");                 // Check the number of tokens                 if (tokens.length < 5) {                     System.out.println("Skipping invalid line: " + line);                     continue;                 }                 try {                     // Read student information                     String lastName = tokens[0];                     String firstName = tokens[1];                     int mid1 = Integer.parseInt(tokens[2].trim());                     int mid2 = Integer.parseInt(tokens[3].trim());                     int finalExam = Integer.parseInt(tokens[4].trim());                     // Calculate the average and grade                     double average = (mid1 + mid2 + finalExam) / 3.0;                     String grade;                     if (average >= 90) grade = "A";                     else if (average >= 80) grade = "B";                     else if (average >= 70) grade = "C";                     else if (average >= 60) grade = "D";                     else grade = "F";                     // Create report string                     String reportString = lastName + "\t" + firstName + "\t" + mid1 + "\t" + mid2 + "\t" + finalExam + "\t" + grade + "\n";                     // Write to report.txt using FileOutputStream                     fos.write(reportString.getBytes());                     // Update exam sum and count for average calculation                     sumMid1 += mid1;                     sumMid2 += mid2;                     sumFinal += finalExam;                     count++;                 } catch (NumberFormatException e) {                     System.out.println("Skipping invalid line with incorrect number format: " + line);                 }             }             // Calculate and write the averages to the file             DecimalFormat df = new DecimalFormat("##.00");             String averageString = "Averages:\tMidterm1 " + df.format(sumMid1 / count) + ",\tMidterm2 " + df.format(sumMid2 / count) + ",\tFinal " + df.format(sumFinal / count);             fos.write(averageString.getBytes());         } finally {             // Close the FileInputStream and FileOutputStream             if (fis != null) {                 fis.close();             }             if (fos != null) {                 fos.close();             }         }     }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.io.IOException;

public class LabProgram {
    public static void main(String[] args) throws IOException {
        Scanner scnr = new Scanner(System.in);
        // Initialize variables for calculating exam averages
        int count = 0;
        double sumMid1 = 0, sumMid2 = 0, sumFinal = 0;

        // Initialize FileOutputStream for output
        FileOutputStream fos = new FileOutputStream("report.txt");

        // Prompt for the filename
        System.out.print("Enter the name of the TSV file: ");
        String fileName = scnr.nextLine();

        FileInputStream fis = null;
        try {
            fis = new FileInputStream(fileName);

            // Read the file and build the content string
            StringBuilder sb = new StringBuilder();
            int ch;
            while ((ch = fis.read()) != -1) {
                sb.append((char) ch);
            }

            // Split the content by lines
            String[] lines = sb.toString().split("\n");

            // Process each line
            for (String line : lines) {
                // Make sure the line contains sufficient data
                if (line.isEmpty()) {
                    continue;
                }

                String[] tokens = line.trim().split("\t");

                // Check the number of tokens
                if (tokens.length < 5) {
                    System.out.println("Skipping invalid line: " + line);
                    continue;
                }

                try {
                    // Read student information
                    String lastName = tokens[0];
                    String firstName = tokens[1];
                    int mid1 = Integer.parseInt(tokens[2].trim());
                    int mid2 = Integer.parseInt(tokens[3].trim());
                    int finalExam = Integer.parseInt(tokens[4].trim());

                    // Calculate the average and grade
                    double average = (mid1 + mid2 + finalExam) / 3.0;
                    String grade;
                    if (average >= 90) grade = "A";
                    else if (average >= 80) grade = "B";
                    else if (average >= 70) grade = "C";
                    else if (average >= 60) grade = "D";
                    else grade = "F";

                    // Create report string
                    String reportString = lastName + "\t" + firstName + "\t" + mid1 + "\t" + mid2 + "\t" + finalExam + "\t" + grade + "\n";

                    // Write to report.txt using FileOutputStream
                    fos.write(reportString.getBytes());

                    // Update exam sum and count for average calculation
                    sumMid1 += mid1;
                    sumMid2 += mid2;
                    sumFinal += finalExam;
                    count++;
                } catch (NumberFormatException e) {
                    System.out.println("Skipping invalid line with incorrect number format: " + line);
                }
            }

            // Calculate and write the averages to the file
            DecimalFormat df = new DecimalFormat("##.00");
            String averageString = "Averages:\tMidterm1 " + df.format(sumMid1 / count) + ",\tMidterm2 " + df.format(sumMid2 / count) + ",\tFinal " + df.format(sumFinal / count);
            fos.write(averageString.getBytes());

        } finally {
            // Close the FileInputStream and FileOutputStream
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
        }
    }
}

Enter the name of the TSV file:
Output is nearly correct, but whitespace differs. See highlights below. Special character legend
Input
Your file content
Expected file content
Student Info.tsv
Barrett Edan
Bradshaw
Charlton
70
45
Reagan
96
Caius 73
61
86
Barrett Edan
Bradshaw
Charlton
70
45
Reagan 96
Caius
73
61
86
59
Mayo
Tyrese 88
Stern Brenda 90
97
94
36
45
59
Mayo
88
Tyrese
Stern Brenda 90
Averages: →Midterm1 83.40, →Midterm2 76.60, Final 61.60
69
97
F
94
36
45
400 00
88
80
AU
D
F
400
88
80
AU
A
B
D
A
B
Averages: Midterm1 83.40, Midterm2 76.60, Final 61.60*
Transcribed Image Text:Enter the name of the TSV file: Output is nearly correct, but whitespace differs. See highlights below. Special character legend Input Your file content Expected file content Student Info.tsv Barrett Edan Bradshaw Charlton 70 45 Reagan 96 Caius 73 61 86 Barrett Edan Bradshaw Charlton 70 45 Reagan 96 Caius 73 61 86 59 Mayo Tyrese 88 Stern Brenda 90 97 94 36 45 59 Mayo 88 Tyrese Stern Brenda 90 Averages: →Midterm1 83.40, →Midterm2 76.60, Final 61.60 69 97 F 94 36 45 400 00 88 80 AU D F 400 88 80 AU A B D A B Averages: Midterm1 83.40, Midterm2 76.60, Final 61.60*
Expert Solution
steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education