My only problem with my code is checking if a file exists. The code works with N
ID: 3688911 • Letter: M
Question
My only problem with my code is checking if a file exists. The code works with Numbers.txt file which is in the project file but if I enter a dummy text file like Words.txt (made up), I get errors and the "The file " + inputFile + "does not exist." message does not appear like it should. Any tips by looking at my code?
This is the
import java.util.Scanner;
import java.io.*;
public class NumberAnalysis
{
//Array of numbers
final private double [] NUMBERS;
public NumberAnalysis(String inputFile)throws IOException
{
NUMBERS = new double[20];
File file = new File(inputFile);
Scanner input = new Scanner(file);
int i = 0;
double total, low, high, average;
/**
* Make sure the file exists.
*/
if(!file.exists())
{
//Display an error message.
System.out.println("The file " + inputFile + " does not exist.");
//Exit the program.
System.exit(0);
}
while(input.hasNext())
{
//Read a value from the file.
NUMBERS[i] = input.nextDouble();
i++;
}
int size = i;
total = NUMBERS[0];
low = NUMBERS[0];
high = NUMBERS[0];
i = 1;
while(i < size)
{
if(NUMBERS[i] > high)
high = NUMBERS[i];
if(NUMBERS[i] < low)
low = NUMBERS[i];
total += NUMBERS[i];
i++;
}
average = total / size;
//Displaying data
System.out.println("Lowest number in Array is: " + low);
System.out.println("Highest number in Array is: " + high);
System.out.println("Total number in Array is: " + total);
System.out.println("Average number in Array is: " + average);
//Close the file.
input.close();
}
}
Explanation / Answer
import java.util.Scanner;
import java.io.*;
public class NumberAnalysis {
// Array of numbers
final private double[] NUMBERS;
public NumberAnalysis(String inputFile) throws IOException {
NUMBERS = new double[20];
File file = new File(inputFile);
int i = 0;
double total, low, high, average;
/**
* Make sure the file exists.
*/
if (!file.exists()) {
// Display an error message.
System.out.println("The file " + inputFile + " does not exist.");
// Exit the program.
System.exit(0);
}
Scanner input = new Scanner(file);
while (input.hasNext()) {
// Read a value from the file.
NUMBERS[i] = input.nextDouble();
i++;
}
int size = i;
total = NUMBERS[0];
low = NUMBERS[0];
high = NUMBERS[0];
i = 1;
while (i < size) {
if (NUMBERS[i] > high)
high = NUMBERS[i];
if (NUMBERS[i] < low)
low = NUMBERS[i];
total += NUMBERS[i];
i++;
}
average = total / size;
// Displaying data
System.out.println("Lowest number in Array is: " + low);
System.out.println("Highest number in Array is: " + high);
System.out.println("Total number in Array is: " + total);
System.out.println("Average number in Array is: " + average);
// Close the file.
input.close();
}
public static void main(String[] args) {
try {
new NumberAnalysis("Numbers.txt");
new NumberAnalysis("Words.txt");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
OUTPUT:
Lowest number in Array is: 35.0
Highest number in Array is: 85.0
Total number in Array is: 1260.0
Average number in Array is: 63.0
The file Words.txt does not exist.
Note :
Change the place of Scanner input = new Scanner(file);
just after checking the file exits or not
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.