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;
        }
}

}

Recommended Answers

All 5 Replies

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.

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

Nnn

So where should I put it at? cgeier

Don't just take code and try to paste it into your program. Spend 10 brief minutes with the relevant tutorial so you will understand what to do, and why...
http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html

@cgeier: It's better to use e.printStackTrace(); in your catch so you get the complete stack as well as the exception error message.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.