E
E
Elxan Mecidli2021-06-07 07:19:37
Java
Elxan Mecidli, 2021-06-07 07:19:37

How to output data from a file to the console?

Hello. I wrote a program where a teacher can register a student, find out all the data of registered students, find students, etc.
I wrote a code that saves and displays student data on the console. But when outputting, the location of the [email protected] object is displayed

public static Student chooseMethod() throws IOException  {
       Scanner sc=new Scanner(System.in);
       int chooseMethod=sc.nextInt();
       if(chooseMethod==1){//Выбираем 1 пункт для регистрации ученика
    sc=new Scanner(System.in);
    System.out.println("Имя ученика");
    String name=sc.nextLine();
    System.out.println("Фамилия ученика");
    String surname=sc.nextLine();
    System.out.println("Возраст ученика");
    int age=sc.nextInt();
    System.out.println("В каком классе учится?");
    sc.nextLine();
    String klass=sc.nextLine();
    
    Student student =new Student();
    student.setName(name);
    student.setSurname(surname);
    student.setAge(age);
    student.setKlass(klass);
    createStudent(student);
    return student;
}
    return null;

   }

   public static void createStudent(Student student) throws IOException {
        FileOutputStream fis=new FileOutputStream("Students.ser");
       ObjectOutputStream ois=new ObjectOutputStream(fis);
       ois.writeObject(student);
       ois.close();
   }

   public static void printStudent() throws IOException, ClassNotFoundException {
        FileInputStream fis=new FileInputStream("Students.ser");
        ObjectInputStream ois=new ObjectInputStream(fis);
        Student student=(Student) ois.readObject();
       System.out.println(student);
        ois.close();
   }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sand, 2021-06-07
@Elxan24-03

You need to override the toString() method in the Student class

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question