I need help with this java problem so it can output like this in the image attached: import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.text.DecimalFormat; import java.util.Scanner; 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: Midterm1 " + df.format(sumMid1 / count) + ", Midterm2 " + df.format(sumMid2 / count) + ", Final " + 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

I need help with this java problem so it can output like this in the image attached:

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

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: Midterm1 " + df.format(sumMid1 / count) + ", Midterm2 " + df.format(sumMid2 / count) + ", Final " + df.format(sumFinal / count);
            fos.write(averageString.getBytes());

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

Input
Your file content
xpected file content
StudentInfo.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
97
94
36
45
Mayo
88
Tyrese
Stern Brenda 90
Averages: Midterml 83.40, Midterm2 76.60, Final 61.60
Mayo Tyres 88
Stern Brenda 90
F
88
80
D
с
59
97
94
36
45
A
B
F
88
80
D
с
A
B
Averages: Midterm1 83.40, Midterm2 76.60, Final 61.60
Transcribed Image Text:Input Your file content xpected file content StudentInfo.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 97 94 36 45 Mayo 88 Tyrese Stern Brenda 90 Averages: Midterml 83.40, Midterm2 76.60, Final 61.60 Mayo Tyres 88 Stern Brenda 90 F 88 80 D с 59 97 94 36 45 A B F 88 80 D с A B Averages: Midterm1 83.40, Midterm2 76.60, Final 61.60
Expert Solution
steps

Step by step

Solved in 4 steps with 4 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
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