Java编码问题

时间:2014-07-15 06:41:50

标签: java arrays multidimensional-array

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

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

这是代码:

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

    }
}

请以任何方式帮助。

5 个答案:

答案 0 :(得分:5)

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

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

或重新抛出

public static void main(String[] args)  throws FileNotFoudnException { 

另见

答案 1 :(得分:2)

将主要方法的定义更改为:

public static void main(String[] args)  throws IOException { 

然后阅读java中的exceptions以了解这意味着什么。

答案 2 :(得分:0)

如果找不到该文件,对Scanner fileIn = new Scanner(hours);的调用可能会失败。在这种情况下,将抛出java.io.FileNotFoundException。 Java在异常处理方面是严格的 - 你不能忽略这个潜在的异常,但必须以某种方式处理它。

这样做的一种方法是在throws方法中添加main子句:

public static void main(String[] args) throws FileNotFoundException {

在这种情况下,如果找不到文件,程序将崩溃。 可能,处理这类问题的更好方法是明确捕获异常:

Scanner fileIn = null;
try {
    File hours = new File("prog2.dat");
    fileIn = new Scanner(hours);
} catch (FileNotFoundException e) {
    System.out.println ("Can't find prog2.dat, quitting");
    System.exit(1);
}

答案 3 :(得分:0)

扫描仪fileIn =新扫描仪(小时); //为阵列分配小时数

这行代码抛出了FileNotFoundException

您有两种方法可以解决此问题:

1)抓住例外

  Scanner fileIn = null;
        try {
            fileIn = new Scanner(hours);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }//assign hours to array

2)抛出异常:

 public static void main(String[] args) throws FileNotFoundException  { 

答案 4 :(得分:0)

创建prog2.dat文件,并在该文件中输入几个小时。

注意:您必须将prog2.dat保存到源目录