For this program, assume you have an input file with several lines of text. Each line has a student's last name, followed by a space and then 4 numbers which represent his/her grades. Your program should read this input file and produce an output file with the same number of lines, each line showing the last name and the average of the student's scores. "grades.txt" is the file your program should read.  "badformat.txt" is an example of a file that your program can't read, so a clear error message should be generated, as shown in the sample output. Your program must catch all exceptions. Your main method must NOT have a "throws" clause - it must catch all exceptions and dea' with them appropriately. Other methods may throw exceptions, as long as the main method catches them. I don't expect you to examine any filenames to make sure they're valid. Instead, you should use the filename the user gives you and try to open a file by that name. That will throw an exception if the filename is invalid, and you should catch that exception. Do not assume there are 6 lines in the input file. Your program should work with any size file, stopping when the end of the file is reached. You may assume there are 4 scores per student, and that each student's data is on its own line. Use the named constant that I provided in the starter code: private static final int NUM_SCORES = 4; for the number of scores per student, to make your code more generalizable. However, if the input file doesn't fit this format, your program should not crash. Instead, you should handle the problem gracefully (maybe output a helpful error message and end the program). Like in the "Programming Exercise for File I/O: Employment Data", I recommend you use the String class's "split" method to help you parse the file. But for this program, instead of splitting the string lineIn on commas: lineIn.split(","), we are splitting on spaces: lineIn.split(" ") Please follow the standard conventions for indentation, meaningful variable names, etc. like the examples in class and in the textbook. Make sure to run your program to test it out (using replit.com or the compiler of your choice), and then submit your Main.java file including your sample output at the bottom, including your output file and the output displayed on the screen. Your job is to write a program to read the grades.txt file in this project, to produce output like shown here. "grades.txt" is the file your program should read.  "badformat.txt" is an example of a file that your program can't read, so a clear error message should be generated, as shown in the sample output. */ import java.io.*; import java.util.Scanner;   class Main { private static final int NUM_SCORES = 4; // named constant for the number of scores per student in file public static void main(String[] args) { } } badformat.txt file Biden 85 90 Trump 50 70 75  Obama 90 95 98 97 Bush 80 70 75 85 Clinton 90 95 85 99Bush 85 95 90 94   grades txt file Biden 85 90 95 98 Trump 50 70 75 80 Obama 90 95 98 97 Bush 80 70 75 85 Clinton 90 95 85 99 Bush 85 95 90 94 /* ***** Sample Output on Repl, hills, or other Linux (specifying an invalid path will throw an exception): ***** Remember that "cat" is a Linux command to display the contents of a text file on the screen.   Welcome to the Grade Average Calculator. Please enter the name of a file to input: trash Unable to open file: trash Please enter the name of a file to input: grades.txt Enter name of output file to create: trash/trash Unable to open file: trash/trash Enter name of output file to create: gradeAvg.txt Average grades saved to gradeAvg.txt   > cat grades.txt Biden 85 90 95 98 Trump 50 70 75 80 Obama 90 95 98 97 Bush 80 70 75 85 Clinton 90 95 85 99 Bush 85 95 90 94   > cat gradeAvg.txt Here are the average grades: Biden 92.0 Trump 68.8 Obama 95.0 Bush 77.5 Clinton 92.3 Bush 91.0   > java Main Welcome to the Grade Average Calculator. Please enter the name of a file to input: badformat.txt Enter name of output file to create: gradeAvg.txt The file badformat.txt isn't in the right format: it should have 1 name followed by 4 numbers on each line, separated by spaces. The following error was generated: 4 scores should follow each name, with spaces in between. Grades could not be averaged. > cat badformat.txt Biden 85 90 Trump 50 70 75 Obama 90 95 98 97 Bush 80 70 75 85 Clinton 90 95 85 99Bush 85 95 90 94 */

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
  • For this program, assume you have an input file with several lines of text. Each line has a student's last name, followed by a space and then 4 numbers which represent his/her grades. Your program should read this input file and produce an output file with the same number of lines, each line showing the last name and the average of the student's scores.
  • "grades.txt" is the file your program should read.  "badformat.txt" is an example of a file that your program can't read, so a clear error message should be generated, as shown in the sample output.
  • Your program must catch all exceptions. Your main method must NOT have a "throws" clause - it must catch all exceptions and dea' with them appropriately. Other methods may throw exceptions, as long as the main method catches them.
  • I don't expect you to examine any filenames to make sure they're valid. Instead, you should use the filename the user gives you and try to open a file by that name. That will throw an exception if the filename is invalid, and you should catch that exception.
  • Do not assume there are 6 lines in the input file. Your program should work with any size file, stopping when the end of the file is reached.
  • You may assume there are 4 scores per student, and that each student's data is on its own line. Use the named constant that I provided in the starter code: private static final int NUM_SCORES = 4; for the number of scores per student, to make your code more generalizable. However, if the input file doesn't fit this format, your program should not crash. Instead, you should handle the problem gracefully (maybe output a helpful error message and end the program).
  • Like in the "Programming Exercise for File I/O: Employment Data", I recommend you use the String class's "split" method to help you parse the file. But for this program, instead of splitting the string lineIn on commas: lineIn.split(","), we are splitting on spaces: lineIn.split(" ")
  • Please follow the standard conventions for indentation, meaningful variable names, etc. like the examples in class and in the textbook.
  • Make sure to run your program to test it out (using replit.com or the compiler of your choice), and then submit your Main.java file including your sample output at the bottom, including your output file and the output displayed on the screen.

Your job is to write a program to read the grades.txt file

in this project, to produce output like shown here.

"grades.txt" is the file your program should read.  "badformat.txt" is an example of a file that your program can't read, so a clear error message should be generated, as shown in the sample output.

*/

import java.io.*;

import java.util.Scanner;

 

class Main

{

private static final int NUM_SCORES = 4;

// named constant for the number of scores per student in file

public static void main(String[] args)

{

}

}

badformat.txt file

Biden 85 90
Trump 50 70 75 
Obama 90 95 98 97
Bush 80 70 75 85
Clinton 90 95 85 99Bush 85 95 90 94

 

grades txt file

Biden 85 90 95 98
Trump 50 70 75 80
Obama 90 95 98 97
Bush 80 70 75 85
Clinton 90 95 85 99
Bush 85 95 90 94

/* ***** Sample Output on Repl, hills, or other Linux

(specifying an invalid path will throw an exception): *****

Remember that "cat" is a Linux command to display the

contents of a text file on the screen.

 

Welcome to the Grade Average Calculator.

Please enter the name of a file to input:

trash

Unable to open file: trash

Please enter the name of a file to input:

grades.txt

Enter name of output file to create:

trash/trash

Unable to open file: trash/trash

Enter name of output file to create:

gradeAvg.txt

Average grades saved to gradeAvg.txt

 

> cat grades.txt

Biden 85 90 95 98

Trump 50 70 75 80

Obama 90 95 98 97

Bush 80 70 75 85

Clinton 90 95 85 99

Bush 85 95 90 94

 

> cat gradeAvg.txt

Here are the average grades:

Biden 92.0

Trump 68.8

Obama 95.0

Bush 77.5

Clinton 92.3

Bush 91.0

 

> java Main

Welcome to the Grade Average Calculator.

Please enter the name of a file to input:

badformat.txt

Enter name of output file to create:

gradeAvg.txt

The file badformat.txt isn't in the right format:

it should have 1 name followed by 4 numbers on each line,

separated by spaces. The following error was generated:

4 scores should follow each name, with spaces in between.

Grades could not be averaged.

> cat badformat.txt

Biden 85 90

Trump 50 70 75

Obama 90 95 98 97

Bush 80 70 75 85

Clinton 90 95 85 99Bush 85 95 90 94

*/

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
File Input and Output 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