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(); } } }
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*](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F6b67a6a2-2d8c-4553-99c2-3c44a5ebdd4f%2F3921d0c1-96e2-4b22-ab57-058ee8670cb4%2Fv3bspcw_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 4 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)