E
E
Elxan Mecidli2021-05-14 07:25:50
Java
Elxan Mecidli, 2021-05-14 07:25:50

Console error during Serialization. How to fix?

Hello. I created a program where the user can register. In order to save data, I use serialization. But the problem is that when reading from a file, the console gives an error. How can I fix this?

This is the registration data.

import java.io.Serializable;
import java.util.Scanner;

public class RegistrationData implements Serializable {
    public String name;
    public String surname;
    public int age;
Scanner sc=new Scanner(System.in);
    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("Введите ваше имя");
        name=sc.nextLine();
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        System.out.println("Введите вашу фамилию");
        surname=sc.nextLine();
        this.surname = surname;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        System.out.println("Ведите ваш возрост");
        int Age= sc.nextInt();
        this.age = age;
    }
}

Created an object of this class in another class
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Scanner;

public class Registration implements Serializable {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
         RegistrationData u = new  RegistrationData();
        u.setName(u.getName());
        u.setSurname(u.getSurname());
        u.setAge(u.getAge());
        try {
            FileOutputStream fis = new FileOutputStream("Users.bin");
            ObjectOutputStream ous = new ObjectOutputStream(fis);
            ous.writeObject(u);
            ous.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

And I created a class where I will find out the data in the file
import java.io.*;
import java.util.Scanner;

public  class Users implements Serializable {
    public static void main(String[] args) {

        try {
            FileInputStream fis=new FileInputStream("Users.bin");
            ObjectInputStream ous=new ObjectInputStream(fis);

Registration newMath=(Registration) ous.readObject();
ous.close();
            System.out.println(newMath);

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



    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rPman, 2021-05-14
@Elxan24-03

java.util.Scanner cannot be serialized
, you can exclude it from serialization by adding the transient keyword in the definition
or concoct your own class based on it by adding the Serializable implementation to it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question