在我的JAVA应用程序中,我需要从另一个类调用一个对象

在我的JAVA应用程序中,我需要从另一个类调用一个对象,java,Java,我正在努力学习Java。我设计了一个关于“学生信息系统”的应用程序,我需要你的帮助 我创建了一个列表,并用NewStudentScreen类存储了新的学生信息。 现在我需要在AskStudentScreen的课堂上显示这些学生信息 我获取学生信息的班级是NewStudentScreen: public void showMainScreen() { System.out.print("studentName: "); String studentName = GeneralUt

我正在努力学习Java。我设计了一个关于“学生信息系统”的应用程序,我需要你的帮助

我创建了一个列表,并用
NewStudentScreen
类存储了新的学生信息。 现在我需要在
AskStudentScreen
的课堂上显示这些学生信息

我获取学生信息的班级是
NewStudentScreen

public void showMainScreen() {

    System.out.print("studentName: ");
    String studentName = GeneralUtil.readConsole();
    System.out.print("studentSurname: ");
    String studentSurname = GeneralUtil.readConsole();
    System.out.print("studentNumber: ");
    String studentNumber = GeneralUtil.readConsole();
    System.out.print("departmentName: ");
    String departmentName = GeneralUtil.readConsole();     
    System.out.print("studentAddress: ");
    String studentAddress = GeneralUtil.readConsole();
    System.out.print("studentTelephone: ");
    String studentTelephone = GeneralUtil.readConsole();

    Student student = new Student();

    student.setName(studentName);
    student.setSurname(studentSurname);
    student.setNumber(studentNumber);
    student.setAddress(studentAddress);
    student.setTelephone(studentTelephone);

    student.setDepartment(student);

    Config.getStudents().add(student);

    System.out.println("");
    System.out.println(student.getName() +" "+student.getSurname()+ " is created.");
    GeneralUtil.readConsole();
    getApplication().gotoMenu(0);
我询问新生信息的班级是
AskStudentScreen

public void showMainScreen() {

     System.out.println("Number of created student is : " + Config.getStudents().size());

    System.out.print("Enter searched students' number: ");
    String ögNo= GeneralUtil.readConsole();


    if(Config.getStudents().contains(ögNo)){
        System.out.println(ögNo+ " found in system. ");
    }
    else
        System.out.println("Student couldn't found.");
我什么都试过了,但总是听到“找不到学生”


完整的源代码可以在上找到。

正如我在评论中所说的,您的
AskStudent
屏幕正在查看
学生
对象列表,以查找
字符串
对象。这将失败,因为这些类型是无法比拟的。此外,您不能只将
equals
hashCode
覆盖添加到
Student
类中,因为
contains
使用搜索参数类型的
equals
hashCode
方法(
String
),因此不会使用它们。相反,请更改您的
AskStudent
检查:

if(Config.getStudents().contains(ögNo)){
    System.out.println(ögNo+ " found in system. ");
}
else
    System.out.println("Student couldn't found.");
为此:

    Student match = Config.getStudents().stream().filter(s -> s.getNumber().equals(ögNo)).findFirst().orElse(null);
    if(match != null){
        System.out.println(ögNo+ " is found: " + match.getName());
    }
    else {
        System.out.println("Student doesn't found.");
    }

现在,它根据传入的数字检查列表中每个
学生的编号。这假设每个学生编号都是唯一的。

您的班级名称在哪里?您是否创建了任何班级?请在此处发布完整代码!。Config.getStudents()返回什么?我们需要查看您的
Student
类。无论如何,我觉得您使用
contains
Student
类型的列表中搜索
String
类型的对象是可疑的。您可能需要在
Student
类中创建一个
equals
方法,如果从控制台读取的名称与该学生的名称匹配,该方法将返回true。我们还需要了解如何创建由
getStudents
方法返回的任何数据结构。@Fredk我认为
Config.getStudents()
必须返回
list
我很高兴我能提供帮助!如果您不介意接受和/或更新此答案,以便其他人将来也能更容易地发现它的有用性,我将不胜感激。@superior,请评论一些合理的东西!StackOverflow是一个专业的问答网站!!用专业的方式表达幸福没有什么错。你仍然可以用字典里拼写的单词来表达你的快乐,并且只使用一个标点符号就足够了。您在技术创业方面还很年轻,@JavaHopper只是想帮助您认识到自己的处境如何,以便被像这样的技术社区所感知。@superior,“No”表示找到答案。但是,我们在这里留下的评论不应该是时髦的或我们通常在发短信时使用的某种英语。你可以自由使用行话。所有这些努力只是为了让这个社区成为一个更好的地方,这样其他用户就不会很难理解实际内容,而不会迷失在一些没有真正帮助的讨论之间。我希望你能理解:)@JavaHopper这个答案太长了。谢谢你的努力我会更加小心的