Language: Java 3. Write a function that takes as input parameters two file names
ID: 3720132 • Letter: L
Question
Language: Java3. Write a function that takes as input parameters two file names “inFileName” and “outFileName” of type String. The function will attempt to open file “inFileName” for reading with try-catch, and then read all true double values. Note that an integer is NOT a true double value. Finally, the function will try to open “outFileName” for writing with try-catch and write to that file the maximum, minimum and average of all double values read.
Language: Java
3. Write a function that takes as input parameters two file names “inFileName” and “outFileName” of type String. The function will attempt to open file “inFileName” for reading with try-catch, and then read all true double values. Note that an integer is NOT a true double value. Finally, the function will try to open “outFileName” for writing with try-catch and write to that file the maximum, minimum and average of all double values read.
3. Write a function that takes as input parameters two file names “inFileName” and “outFileName” of type String. The function will attempt to open file “inFileName” for reading with try-catch, and then read all true double values. Note that an integer is NOT a true double value. Finally, the function will try to open “outFileName” for writing with try-catch and write to that file the maximum, minimum and average of all double values read.
Explanation / Answer
/**The java program that open an input file sample_data.txt
* and then write only double values and maximum and minimum
* values to the output file name.*/
//Assignment.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class Assignment
{
public static void main(String[] args)
{
//Set input and output file names
String inFileName="sample_data.txt";
String outFileName="true_double.txt";
//call read method with input and output string file names
read(inFileName,outFileName);
}
/*
* The read method that reads in file and out file
* and read only double values and find maximum and minimum
* values.
* */
private static void read(String inFileName, String outFileName) {
Scanner filereader=null;
PrintWriter writer=null;
double max=Double.MIN_VALUE;
double min=Double.MAX_VALUE;
//try-catch block to catch file not found exception
try
{
//Open file for reading
filereader = new Scanner(new FileReader(inFileName));
//Open file for writing
writer=new PrintWriter(new File(outFileName));
//read until end of file
while (filereader.hasNextLine())
{
/*
* read the next integer is true then read
* the integer and ignore
* */
if(filereader.hasNextInt())
{
filereader.next();
}
else
{
double num=filereader.nextDouble();
if(num>max)
max=num;
if(num<min)
min=num;
writer.printf("%5.2f ",num);
}
}
/*
* write maximum and minimum.*/
writer.printf("Maximum : %5.2f ",max);
writer.printf("Minimum : %5.2f ",min);
/*close the stream class object.*/
filereader.close();
writer.close();
//Print Done!
System.out.println("Done!");
}
catch(FileNotFoundException ex)
{
System.out.println(ex.getMessage());
}
}//end of main method
} //end of the class
-----------------------------------------------------------------------------------------------------
Input File Name : sample.txt
25.0
12
21.0
30.0
15
16
11
12.0
15.0
54.25
15
18
------------------------------------------------------------------------
Sample Output:
Done!
----------------------------------------------------------------------------
Output text file :
true_double.txt
25.00
21.00
30.00
12.00
15.00
54.25
Maximum : 54.25
Minimum : 12.00
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.