java中文本查找学生信息

我把文本中的学生信息写入了一个集合,但是查找时,却无法查找成功
private static String[] student;

public static void R(String n) throws IOException{
    Student stu = new Student();
    File file = new File("D:\\","aa.txt");
    FileInputStream intput = new FileInputStream(file);
    BufferedReader br = new BufferedReader(new InputStreamReader(intput));
    String t;
    List<String>list = new ArrayList<>();
    while ((t=br.readLine())!=null){
        list.add(t);

    }
    for (String students:list) student = students.split(",");
    for (String students:list){
        if (students.equals(n)){
            System.out.println(stu+"查找成功!");
            }
        else{
                System.out.println("该学生不存在");
                break;
            }
    }

}

public static void main(String[] args) {
    System.out.println("输入你要查找的学生姓名:");
    Scanner scanner = new Scanner(System.in);
    String n = scanner.nextLine();
    try {
        R(n);

    } catch (IOException e) {
        e.printStackTrace();
    }

}

img

img


这段应该有问题.你debug看下这里的值

楼主,逻辑有问题。你在集合里查询有没有匹配,不应该只查找第一个就判定没有就是失败,而应该等查的过程都没有找到才能断定是失败。
boolean isHas = false;
for (String students:list){
if (students.equals(n)){
System.out.println(stu+"查找成功!");
isHas = true;
break;
}
}
if( !isHas ){
System.out.println("该学生不存在");
}