Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在文件中搜索学生_Java_Api_File Io - Fatal编程技术网

Java 在文件中搜索学生

Java 在文件中搜索学生,java,api,file-io,Java,Api,File Io,我想搜索学生的输入文件。以下是我迄今为止工作的概述 类学生,具有setter、getter和显示方法print 班级学生档案 这个方法没有显示任何内容,我不知道为什么。你有什么见解吗 您正在从文件中读取行并将其内容分配给str,但从未使用此值执行任何操作 而且,学生似乎是空的 假设您的文件仅包含学生的ID: BufferedReader read = new BufferedReader(new FileReader(file)); String str; while((str

我想搜索学生的输入文件。以下是我迄今为止工作的概述

类学生,具有setter、getter和显示方法print

班级学生档案


这个方法没有显示任何内容,我不知道为什么。你有什么见解吗

您正在从文件中读取行并将其内容分配给str,但从未使用此值执行任何操作

而且,学生似乎是空的

假设您的文件仅包含学生的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());
     }

另外,确保你在Student中的print方法真正打印出你想要的内容。

你的输入文件是什么样子的?这些代码甚至不会编译。如果你创建一个简单的示例,我们可以提供帮助。看见
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());
     }