JAVA (Don't make it too complicated) Write a program that prompts the user for the name of a text file. The file should consist of a sequence of integers, one integer per line. The program will read in each line (using nextLine()), parse it as an int (using Integer.parseInt()), and report the number of values and their average. The average should be computed as a double.
Your program should do some basic exception handling:
(1) If the file cannot be found then the program should print a message to that effect and terminate by executing System.exit(1).
(2) If the file can be found then every time that the program reads in a line that cannot be parsed (i.e. Integer.parseInt() throws a NumberFormatException) the program should print an error message with the bad line.
The final printout should display the number of good (i.e. parsable) lines, the average of the parsable values as a double, and the number of bad (unparsable) lines. Two sample runs are shown below:
Enter name of input file: numbers.txt
Cannot parse eight as an integer.
Cannot parse seven as an integer.
Cannot parse eighty-five thousand and sixty-two as an integer.
Cannot parse 13 98 as an integer.
Number of parsable lines: 6
Average value: 80.83333333333333
Number of unparsable lines: 4
Enter name of input file: Nums.txt
Could not find file: Nums.txt
The values in numbers.txt were as follows:
•5
• 15
• 312
Thanks for the question. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks =========================================================================== import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class GameItemTest { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter name of input file: "); String fileName = scanner.nextLine(); double total = 0; int countIntegers = 0; int unParsableLines = 0; try { Scanner fileReader = new Scanner(new File(fileName)); while (fileReader.hasNext()) { String line = fileReader.nextLine(); try { int toInteger = Integer.parseInt(line); total += toInteger; countIntegers += 1; } catch (NumberFormatException nfe) { //program should print an error message with the bad line. System.out.println("Cannot parse " + line + " as an integer."); unParsableLines++; } } //The final printout System.out.println("Number of parasable lines: " + countIntegers); if (countIntegers != 0) { System.out.println("Average of all integers: " + (total / countIntegers)); } System.out.println("Number of unparsable lines: " + unParsableLines); fileReader.close(); } catch (FileNotFoundException e) { //(1) If the file cannot be found then the program should // print a message to that effect and terminate by executing // System.exit(1). System.out.println("Could not find file: " + fileName); System.exit(1); } } }
=================================================================
Get Answers For Free
Most questions answered within 1 hours.