gpt4 book ai didi

java - 错误 : unreported exception java. io.FileNotFoundException;必须被捕获或宣布被扔出

转载 作者:行者123 更新时间:2023-12-02 00:05:46 25 4
gpt4 key购买 nike

我正在尝试为我的 java 项目编写电话簿(MATSEC 'O' Levels 相当于英国 GCSE),并且在编码(使用 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。

最佳答案

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

(或)

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

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

关于java - 错误 : unreported exception java. io.FileNotFoundException;必须被捕获或宣布被扔出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13920527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com