2012-12-17 313 views
0

我想为我的Java项目(MATSEC“O”水平相当于英国GCSE)的代码,电话簿和编码时(使用BlueJ的)这个错误弹出。我使用我的老师的书作为参考,没有任何与错误有关,并没有说我应该添加任何东西。这是我的Java代码(不是主类):错误:未报告的异常java.io.FileNotFoundException;必须捕获或声明抛出

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。

+1

如果老师不理解检查异常,他应立即更换**。 – SLaks

+0

这是您为文件IO学习的第一件事情之一。如果您的教师无法执行文件IO,那么该升级了。 – Thomas

+0

你的老师不知道如何解决这个问题?你是认真的吗。相信我,你的编程生涯处于危险之中,直到你改变你的老师。 –

回答

2

你的文件读出的代码内读()方法应该try/catch块

(或)

定义读取()方法read() throws FileNotFoundException { .....}内缠绕。

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

+0

感谢您的回复。另一个问题出现了。同样的错误,但是这次重要的是()s: 'name [entnum] = txt.readLine();' 这与上面的'for'循环一样。我应该用括号...在括号中吗?谢谢@Nambari –

+0

可能是你缺少导入语句。 – kosa

+0

对不起,刚刚完成评论作为evrytime我按下输入提交它。我应该在括号中使用'throws ...'吗? –

1

请将您的Buffered Reader放在try catch块中:)

+0

和'赶上(FileNotFoundException异常FE)':) – Parth

0

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

+0

什么用BlueJ的问题? –

+0

我想的BlueJ和Idea之间的差异(或者Eclipse,但我用的主意)是,当我在IDE中插入代码它说我,因为没有异常代码的代码不编译:) – Dedyshka

相关问题