0

我正在尝试为我的 java 项目(相当于英国 GCSE 的 MATSEC 'O' Levels)编写电话簿,并且在编码(使用 BlueJ)时弹出此错误。我正在使用我老师的书作为参考,没有任何与错误相关的内容,也没有说我应该在其中添加任何内容。这是我的java代码(不是主类):

import java.io.*;

class Data{
    String read(){  

        String[] name = null;
        String[] surname = null;
        String[] company = null;
        String[] house = null;
        String[] street = null;
        String[] locality = null;
        String[] telno = null;
        String[] mobno = null;
        int entnum;



        BufferedReader txt = new BufferedReader(new FileReader("Directory.txt"));

        System.out.println("Name\tSurname\tCompany\tHouse\tStreet\tLocality\tTelephone\tMobile");
            System.out.println("\n-----------------------------------------------------------------------------------------------");

        for(entnum = 0;name[entnum]!= null; entnum++){
            name[entnum] = txt.readLine();
            surname[entnum] = txt.readLine();
            company[entnum] = txt.readLine();
            house[entnum] = txt.readLine();
            street[entnum] = txt.readLine();
            locality[entnum] = txt.readLine();
            telno[entnum] = txt.readLine();
            mobno[entnum] = txt.readLine();

            System.out.print(name[entnum]+ "\t");
            System.out.print(surname[entnum]+ "\t");
            System.out.print(company[entnum]+ "\t");
            System.out.print(house[entnum]+ "\t");
            System.out.print(street[entnum]+ "\t");
            System.out.print(locality[entnum]+ "\t");
            System.out.print(telno[entnum]+ "\t");
            System.out.print(mobno[entnum]+ "\t\n");

            }
        return null;
    }
}

基本上,这只是从文本文件中读取并显示条目。我还没有使用 GUI。

4

3 回答 3

2

您在 read() 方法中的文件读取代码应包含在 try/catch 块中

(或者)

将 read() 方法定义为 read() throws FileNotFoundException { .....}.

FileNotFoundException是检查异常,它应该在 throws 子句中声明(或)由于catch/specify要求,可能抛出此异常的代码应该包装在 try/catch 中。

于 2012-12-17T19:11:23.210 回答
1

请将您的缓冲阅读器放入 try catch 块:)

于 2012-12-17T19:11:45.190 回答
0

尝试使用现代 IDE,例如 Eclipse。它将帮助您检测许多编译错误。

于 2012-12-17T21:08:54.030 回答