I have an assignment for my cps class in which we have to create a program that will import a file from a server that has a bunch of numbers in it. The program is suppose to take those numbers, sort them, give the minimum and maximum value, average, median, and print the data. But when I compile it, it states that "java:12: error: unreported exception FileNotFoundException; must be caught or declared to be thrown". What does it mean???

import java.util.*;
import java.io.*;
public class sorting

public static void main(String[]args)
{        


        double num[]=new double[1000];
        int n;
        double average,median;
        n=getdata(num);
        sortdata(num,n);
        average=getAverage(num,n);
        median=getMedian(num,n);
        printdata(num,n,average,median);
        }

public static void printdata(double num[],int n,double average,double median)
        {
        int i;
        System.out.print("The sorted numbers are");
        for(i=0;i<n;i++)
                {if(i ==0)
                        System.out.println();
                        System.out.print(num[i]+"\t");
}
System.out.println();
System.out.println("Range: ["+num[0]+", "+num[n-1]+"]");
System.out.println("Average: "+average);
System.out.println("Median: "+median);
}
public static double getMedian(double num[],int n)
        {
        if(n%2==0)
                return (num[n/2]+num[n/2-1])/2.;
        else
                return num[n/2];

        }

public static double getAverage(double num[],int n)
        {int i=0;
double sum=0,average;
        for(i=0;i<n;i++)
        sum+=num[i];
        average=sum/n;
        return average;
        }
public static int getdata(double num[])throws FileNotFoundException
        {
        String filename;
        int n,i;
        Scanner input=new Scanner (System.in);
        System.out.print("Enter name of file:");
        filename=input.nextLine();
        Scanner in=new Scanner(new File(filename));
        n=in.nextInt();
        for(i=0;i<n;i++)
                num[i]=in.nextDouble();
        return n;
        }
public static void sortdata(double num[],int n)
        {int i,j;
double t;
        for(i=0;i<n-1;i++)
        for(j=i+1;j<n;j++)
        if(num[i]>num[j])
        {t=num[i];
        num[i]=num[j];
        num[j]=t;
        }
}

}

Need to use exception handling.

try
{
   //run your code here...

}
catch (FileNotFoundException e) {
    System.err.println("FileNotFoundException: " + e.getMessage());

    //do whatever you want to do when 
    //this exception happens
}//

Click Here for documentation.

Edited 2 Years Ago by cgeier

Always capitalize class names. This is a universally-observed convention. Failure to do so confuses other programmers.

This article has been dead for over six months. Start a new discussion instead.