Question: Problem 2 50 pts: Write a program In Java that prompts the user for the name of a text file. The program will then read
Problem 2 50 pts: Write a program In Java that prompts the user for the name of a text file. The program will then read the contents of the file, process the content, and create a report accordingly. Each line in the file should consist of a students last name, a space, and a test score (an integer between 0-100); some lines can however be malformed. The behavior of the program should be as follows: - If the file cannot be found, then the program will print a message to that effect and terminate by executing System.exit(1). - The program will read in each line (using nextLine()), split the line into tokens, ignore the first token -- the student name, and (attempt to) use the second token as an integer -- the test score. - The program should handle bad input lines, which either do not have two tokens (a string and an integer), or they do have two tokens but the second is not an integer (an attempt to parse the second token as an integer by using Integer.parseInt() causes a NumberFormatException.) - Every time the program reads in a line that cannot be parsed (as described above), the program should print an error message with the bad line, and then continue reading the rest of the lines. - The program will calculate the average of the integers read from the file. The average should be computed as an integer; if there are only bad input lines in the file, then the program should print a message and continue. - The program will also report the number of input lines read from the file which were malformed, and the number of input lines read from the file which were correctly parsed. - Examine the sample output below, and include the necessary texts for the user prompts and other outputs. - Create a text file with sample input lines, and place it in the project directory to test your program. - Hint: You can start working on this problem by reviewing the ReadData.java example source code. Run 1: Enter name of input file: scores.txt Cannot parse 'eighty' as an integer Line does not have two items: 'null' Average of grades is 93 Number of parsable lines: 3 Number of unparsable lines: 2 (Content of file scores.txt was: Smith 90 Jones eighty McMillan 100 null Werner 90) Run 2: Enter name of input file: scores.txt Cannot parse 'eighty' as an integer Average of grades can't be calculated, there are no grades. Number of parsable lines: 0 Number of unparsable lines: 1 (Content of file scores.txt was: Jones eighty) Run 3: Enter name of input file: abc.txt Source file abc.txt does not exist Process finished with exit code 1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
