Java编码问题

1 java arrays multidimensional-array

我正在为我的"java简介"编程课程做一个代码,我遇到了一个错误,我真的不知道如何修复.我一直有错误说:

unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
                Scanner fileIn = new Scanner(hours);
Run Code Online (Sandbox Code Playgroud)

这是代码:

import java.util.Scanner;

import java.io.File;

public class program2{

    public static void main(String[] args)  { 

        int[][] array = new int [8][7];
        int[] total = new int [array.length];
        int[] finalTotal = new int[array.length];
        int[] employees = {0,1,2,3,4,5,6,7};


        File hours = new File("prog2.dat"); //read file
        Scanner fileIn = new Scanner(hours);//assign hours to array


        while(fileIn.hasNext()){
            for(int i = 0; i < 8; i++) {
                for (int a = 0; a < 7; a++) {
                    int num =fileIn.nextInt();
                        array[i][a] = num;
            }
        }
    }

    fileIn.close(); //Closes file

    // takes employees hour total
    for(int i=0; i < 8; i++) {
        total[i] = array[i][0] + array[i][1] + array[i][2] + 
        array[i][3] + array[i][4] + array[i][5] + array[i][6];
        }

    // takes the hours and sorts from greatest to least
    for(int i= 0; i < 7; i++) {
        int greatest = total[i];

        for(int b = i+1; b < 8; b++) {
            if(total[b] > greatest) {
                int employeeTemp = employees[i];
                employees[i] = employees[b];
                employees[b] = employeeTemp;
                int tempNum = greatest;
                greatest = total[b];
                total[i] = greatest;
                total[b] = tempNum;
            }
        }
    }

    // print employee number and worked hours
    for(int i = 0; i < 8; i++) {
        System.out.println(" Employee #" + employees[i] + ": " +
        total[i]);
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

请以任何方式帮助您.

Jig*_*shi 5

Scanner(fileInstance) 声明抛出一个FileNotFoundException所以你必须处理它

    Scanner fileIn = null;
    try{
       fileIn = new Scanner(hours)
    }catch(FileNotFoundException e){
        // write code that handles when this exceptional condition raises
    }
Run Code Online (Sandbox Code Playgroud)

或重新抛出它

public static void main(String[] args)  throws FileNotFoudnException { 
Run Code Online (Sandbox Code Playgroud)

另见