Please make this code easy to understand. This is suppose to be a low level comp
ID: 3775037 • Letter: P
Question
Please make this code easy to understand. This is suppose to be a low level comp sci course so dont use advanced programming.
Part 1
Write a program that reads a stream of integers from a file and writes only the positive numbers to a second file. The user should be prompted to enter the names of both the input file and output file in main(), and then main() should attempt to open both files (providing an error if there is an error during this process). The main() method should then call the process() method to read all the integers from the input file and write only the positive integers to the output file. The process() method takes as arguments a Scanner to read from the input and a PrintWriter to write to the output. You can assume that if you are able to successfully open the input file, then there will only be integers in it.
Part 2
Write a program that reads a stream of integers from a file and prints to the screen the range of integers in the file (i.e. [lowest, highest]). You should first prompt the user to provide the file name. You should then read all the integers from the file, keeping track of the lowest and highest values seen in the entire file, and only print out the range of values after the entire file has been read.
Importantly, unlike the previous problem, you can make no assumptions about the contents of the file. If your program cannot read the file, opens an empty file, or encounters a non-integer while reading the file, your program should output that the file is invalid.
Test for several invalid files (e.g. non-existent, empty, non-integer) as well as files that contain only integer values.
Explanation / Answer
import java.util.*;
import java.io.*;
public class Driver{
//function to read from file and write to output file
public static void process(Scanner input,PrintWriter output)
{
//read from file and write to output
while (input.hasNextLine()) {
String line = input.nextLine();
if(Integer.parseInt(line)>0)
output.println(line);
}
}
public static void main(String []args){
//prompt user for file names
Scanner read=new Scanner(System.in);
System.out.print("Enter input file name: ");
String ifile=read.next();
System.out.print("Enter output file name: ");
String ofile=read.next();
//create file objetcs
File f1 = new File(ifile);
File f2 = new File (ofile);
//create file reader and writer variables
Scanner sc=null;
PrintWriter pw=null;
//open input file pointer and throw exception if not found
try
{
sc = new Scanner(f1);
}
catch (FileNotFoundException e)
{
System.out.println("Unable to open input file");
}
//open output file pointer and throw exception if not found
try
{
pw = new PrintWriter (f2);
//call process function
process(sc,pw);
//close pointers
pw.close();
sc.close();
}
catch (FileNotFoundException e)
{
System.out.println("Unable to open output file");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Please post different questions for diffeerent programs
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.