HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavaMajor

Unbreakable average program

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
unbreakableprogramaverage

Problem

I'm creating a program that averages four numbers - three from a file and one from user input. Simple enough. But the catch is, the professor is going to be deliberately trying to break the program.

I'd like advice in any regard, but remember the focus is on robustness.

```
import java.util.*;
import java.io.*;

public class average {
public static void main (String[] args) throws IOException{
double[] nums = new double[4];
nums = inputHandler();
System.out.println("Average " + findAverage(nums));

System.out.println("End Program.");
}

public static double findAverage(double[] nums){
double average = 0.0;
final double N = 4.0;

for(int i = 0; i < N; i++){
average += nums[i] / N;
}

return average;
}

public static double[] inputHandler() throws IOException{
Scanner input = new Scanner(System.in);
double[] nums = new double[4];
double[] fileNums = new double[3];

// get first three numbers from file
System.out.print("Enter the name of the file which contains the first three numbers: ");
fileNums = fileInput(input.nextLine());
System.out.println();

// copy numbers from fileNums[] to nums[]
for(int i = 0; i < 3; i++){
nums[i] = fileNums[i];
}

//get last number from user input
System.out.print("Enter the fourth number: ");
nums[3] = userInput(input.nextLine());
System.out.println();

input.close();

return nums;
}

public static double[] fileInput(String fileName) throws IOException{
double[] nums = new double[3];

System.out.println();

File file = new File(fileName);

if(!file.isFile()){
System.err.println("ERROR: File does not exist.");
System.exit(-1);
}

System.out.println(fileName + ":");

Scanner input = new Scanner(file);

Solution

First, use try/catch blocks around parts that can throw exceptions. For example, what happens when an IOException is thrown by the file, perhaps because the data is corrupted and the file exists, but cannot be opened?

Second, I don't know what constitutes failure, but an app-closing failure is failure in my book:

if(!file.isFile()){
    System.err.println("ERROR: File does not exist.");
    System.exit(-1);
}


Third, @SirPython is right - use the built-in method for reading your number. There are built-in functions provided for reading both doubles and ints, whichever your program needs.

Fourth, this will not necessarily help robustness, split your method logic up. You are doing a lot of printing in your fileInput() method. Do your printing in a dedicated method. Do your input in a dedicated method. Feel free to throw an exception when exceptional behavior happens, but be sure to handle the exception. Split your logic up into bite-sized components and implement them separately - this will make reuse easy.

Fifth, recover from your errors. If you need, start the entire input processes over, but do not close the program. Closing the program because of an error will probably be counted as an error, and is unacceptable in many programs.

Code Snippets

if(!file.isFile()){
    System.err.println("ERROR: File does not exist.");
    System.exit(-1);
}

Context

StackExchange Code Review Q#104871, answer score: 24

Revisions (0)

No revisions yet.