The following code calculates the average of numbers that are stored in a text file. I have added some exception handling for "file not found" errors. I need to know how to add another exception for when the data in the text file is not numeric (or not int). I thought about adding multiple catches. Not sure how though?
import java.util.*;
import java.io.*;
public class NumAvg2 {
public static void main(String[] args)
{
int c = 1;
long sum = 0;
String strFileName;
strFileName = args[0];
Scanner scFileData;
try{
scFileData = new Scanner (new File(strFileName));
while (scFileData.hasNext())
{
sum = sum + scFileData.nextInt();
c++;
}
scFileData.close();
System.out.println("Number of integers: " + (c - 1));
System.out.println( "Average = " + (float)sum/(c - 1));
} catch (FileNotFoundException e) {
System.out.println("File not found!");
System.exit(1);
}
}
}