Answer the question
In order to leave comments, you need to log in
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;
}
}
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();
}
}
}
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question