Java program question ing Exercise 12.14. Write a program that prompts or the Th
ID: 3877539 • Letter: J
Question
Java program question
ing Exercise 12.14. Write a program that prompts or the This problem is based on Programming name of a text file. The file should consist of a sequence of integers, one integer per line. The progranm will read in each line (using nextLine0), parse it as an int (using Integer.parselnt0), and report the number of values and their average. The average should be computed as a double the user f 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.parselnt0 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: 15 312 16 eight seven eighty-five thousand and sixty-two 13 98 93Explanation / Answer
program:--
import java.io.File;
import java.io.FileNotFoundException; //for file not found exception
import java.util.Scanner; //for reading inputs console/file
public class FileReader
{
public static void main(String[] args) throws FileNotFoundException, NumberFormatException //throws the exceptions
{
double average; //average as double
int sum=0,parselines=0,notparselines=0; //for pares line, non pares lines, sum for integers
String st = null; //for read string from file
File file; //for file
Scanner sc = null; //initilize scanner
String filename; //for file name
Scanner scan=new Scanner(System.in); // for reding from console
System.out.println("Enter name of input file: ");
filename=scan.nextLine(); //read console
try //try for file existancy
{
file = new File(filename); //for reading from file
sc = new Scanner(file); //reading file
}
catch(FileNotFoundException e) //catch for exception if file not found
{
System.out.println(e); //print reason for file not found
}
while (sc.hasNextLine()) //check for next line in file
{
st=sc.nextLine(); //scan the next line in file
try //try to convert string to integer
{
int number=Integer.parseInt(st); //converting string to integer
sum=sum+number; //sum the converted integer
parselines++; //count the parsaple lines
}
catch(NumberFormatException e)
{
System.out.println("Cannot parse "+st+" as an integer."); //print the non parsable lines
notparselines++; //count the nonparsable lines
}
}
System.out.println("Number of parsable lines: "+parselines); //print number of parsable lines
average=Double.valueOf(sum)/Double.valueOf(parselines); //convert the number of parsable lines and sum to double and calculate average
System.out.println("Average value: "+average); //print average
System.out.println("Number of unparsable lines: "+notparselines); //print number of nonparsable lines
}
}
output:--
run:
Enter name of input file:
C:\Users\Username\Documents\FileReader\src umbers.txt
Cannot parse eight as an integer.
Cannot parse seven as an integer.
Cannot parse eight-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
BUILD SUCCESSFUL (total time: 15 seconds)
run:
Enter name of input file:
myfile
java.io.FileNotFoundException: myfile (The system cannot find the file specified)
Exception in thread "main" java.lang.NullPointerException
at FileReader.main(FileReader.java:27)
C:UsersPhaneendraAppDataLocalNetBeansCache8.2executor-snippets un.xml:53: Java returned: 1
BUILD FAILED (total time: 7 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.