//necessary imports for file i/o
import java.io.FileInputStream; //"turns" the file into a read stream
import java.io.IOException;
import java.io.File;
import java.io.FileOutputStream; //"turns" the file into a write stream
import java.io.PrintWriter; //writes to the stream
import java.io.FileNotFoundException; //this exception must be caught and handled when dealing with streams!
import java.util.Scanner; //reads from stream
/**
* Write a description of class Stats here.
*
* @author
* @version 2/15/2023
*/
public class Stats
{
public static void main(final String[] args)
//throws IOException
{
// Scanner and PrintWrite must be declared outside the try block
// otherwise their scope will be limited to within the block
FileInputStream fis = null;
Scanner input = null;
double inputNumber = 0.0d;
int negNum = 0;
int btw0and100 = 0;
int geq100 = 0;
int lineCounter = 0;
double grandTotal = 0.0d;
double min = 0.0d;
double max = 0.0d;
double average = 0.0d;
try
{
fis = new FileInputStream("fileIn.txt");
input = new Scanner(fis);
while (input.hasNext() && input.hasNextDouble())
{
inputNumber = input.nextDouble();
//String line = input.nextLine();
//String str = new String(line);
++lineCounter;
if (inputNumber < 0)
{
++negNum;
}
else
{
if (inputNumber > 100)
{
++geq100;
}
else // if (inputNumber >= 0 && inputNumber < 100)
{
++btw0and100;
}
}
}
final boolean success = display(average, max, min, lineCounter, negNum, btw0and100, geq100);
if (success)
{
System.out.println(negNum + " " + btw0and100 + " " + geq100);
}
}
catch (IOException ioex)
{
System.out.println("File not found.");
ioex.printStackTrace();
//System.exit(0);
}
catch (Exception ex)
{
System.out.println("Unexpected situation");
ex.printStackTrace();
//System.exit(0);
}
finally
{
if (input != null) try { input.close(); } catch(Exception ex) {}
if (fis != null) try { fis.close(); } catch(Exception ex) {}
}
}
public static boolean display(
final double average,
final double max,
final double min,
final int lineCounter,
final int negNum,
final int btw0and100,
final int geq100)
{
FileOutputStream fos = null;
PrintWriter output = null;
try
{
fos = new FileOutputStream("fileOut.txt");
output = new PrintWriter(fos, true);
output.println("Average: " + average);
output.println("Max: " + max);
output.println("Min: " + min);
output.println("Lines: " + lineCounter);
output.println("Numbers below zero: " + negNum);
output.println("Numbers between 0 and 100: " + btw0and100);
output.println("Numbers greater than 100: " + geq100);
}
catch (IOException ioex)
{
System.out.println("Sorry, we cannot locate the file!");
ioex.printStackTrace();
return false;
}
catch (Exception ex)
{
System.out.println("Unexpected situation");
ex.printStackTrace();
return false;
}
finally
{
if (output != null) try { output.close(); } catch(Exception ex) {}
if (fos != null) try { fos.close(); } catch(Exception ex) {}
}
return true;
}
}
1
2.3
3.4
5
Exception
5
asdf
7 apple
21 + 2
Average: 0.0
Max: 0.0
Min: 0.0
Lines: 4
Numbers below zero: 0
Numbers between 0 and 100: 4
Numbers greater than 100: 0