Write a program in java that reads a stream of integers from a file and prints t
ID: 3763736 • Letter: W
Question
Write a program in java 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.
Skeleton:
boolean goodFile = false;
int min = 0, max = 0;
try (Scanner sFile = new Scanner(new File(iFile))) {
max = min = sFile.nextInt();
//details
goodFile = true;
} catch (FileNotFoundException e) {
} catch (InputMismatchException e) {
} catch (NoSuchElementException e) {
}
Explanation / Answer
/*************************
* Write a program in java 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.
*/
import java.util.*;
import java.io.*;
public class Search_integers{
public static void main(String[] args)throws FileNotFoundException
{String iFile;
int min = 0, max = 0;
int i=0,auxMax,auxMin;
Scanner in=new Scanner(System.in);
System.out.print("Enter input file name: "); // You should first prompt the user to provide the file name.
iFile=in.next();
try{Scanner sFile=new Scanner(new FileReader(iFile));
if(!sFile.hasNext())
{System.out.println(iFile+" the file is invalid");
System.exit(1);
}
System.out.println("The integers are:");
while (sFile.hasNext())
{
if (sFile.hasNextInt())
{auxMin=auxMax=sFile.nextInt();//* You should then read all the integers from the file,
// System.out.println(auxMin);
/*****
keeping track of the lowest and highest values seen in the entire file,
*****/
if(i==0)
min=auxMin;
if (auxMax>=max)
max=auxMax;
if(auxMin<=min)
min=auxMin;
i++;
}
else
{
sFile.next();}
}
//print out the range of values after the entire file has been read
System.out.println("The lowest: " +min +"the highest: "+ max);
sFile.close();
System.exit(0);
}catch ( FileNotFoundException e)
{System.out.println("the file is invalid");
System.exit(2);
}
}
}
/*****************************************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.