2014-09-10 36 views
-5

我想在输入文件中搜索Student s。以下是我迄今为止的工作概况。搜索文件中的学生

Class Student,与setter方法,getter和显示方法(打印)

Class StudentFile

import java.io.*; 
import java.util.ArrayList; 
public class StudentFile { 

public void Trouver(int id) 
    { 
     try 
     { 
     File file = new File("C:/Users/Akram/Documents/akram.txt"); 
     BufferedReader read = new BufferedReader(new FileReader(file)); 
     String str; 
     while((str=read.readLine())!=null) 
     { 
      Student s; 

      if(s.getId()==id) 
       System.out.println(s.print()); 
     } 
     read.close(); 
      } 
     catch(IOException e) { System.out.println(e); } 
    } 
} 

这种方法就什么都不显示了,我不知道为什么。你有什么见解吗?

+0

你的输入文件是什么样的? – 2014-09-10 17:51:11

+0

这段代码甚至不会编译。 – 2014-09-10 17:54:28

+0

如果您创建一个最简单的示例,我们可以提供帮助。请参见[如何创建最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 – DavidPostill 2014-09-10 19:09:39

回答

1

您正在读取文件中的行并将其内容分配到str,但您绝不会对此值做任何操作。

此外,Student似乎是空的。

假设你的文件包括学生的唯一的ID:

BufferedReader read = new BufferedReader(new FileReader(file)); 
    String str; 
    while((str=read.readLine())!=null) 
    { 
     // Your constructor assigns str to ID property of Student 
     // Casting to Integer, because ID is a number 
     Student s = new Student(Integer.valueOf(str)); 

     if(s.getId()==id) 
      System.out.println(s.print()); 
    } 

此外,请确保您的学生print()方法真的打印你想要什么。