gpt4 book ai didi

java - readObject() 方法不打印对象

转载 作者:行者123 更新时间:2023-11-30 08:00:27 26 4
gpt4 key购买 nike

在我必须做的作业中,我必须编写一个学生对象,其中包含姓名、社会安全号码、通过的类(class)数量以及每门类(class)的成绩。我创建了一个 gpa 方法来计算 gpa,并用我自己的方法重写了 writeObject 和 readObject 方法。 Here 是完整的源代码 - http://pastebin.com/dNMrc1zK

这就是每个实例没有 getter 和 setter 方法时的样子:

import java.io.*;

public class Student implements Serializable{
private String name;
private int ssNum;
private int coursesCompleted;
private char[] grades;

public Student(String n, int num, int c){
n = name;
num = ssNum;
c = coursesCompleted;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getSsNum() {
return ssNum;
}

public void setSsNum(int ssNum) {
this.ssNum = ssNum;
}

public int getCoursesCompleted() {
return coursesCompleted;
}

public void setCoursesCompleted(int coursesCompleted) {
this.coursesCompleted = coursesCompleted;
}

public char[] getGrades() {
return grades;
}

public void setGrades(char[] grades) {
this.grades = grades;
}
public double gpa(char[] grades){

double gpa = 0;

for (int i = 0; i < grades.length; i++){
if (grades[i] == 'A'){
gpa += 4.0;
}
else if (grades[i] == 'B'){
gpa += 3.0;
}
else if (grades[i] == 'C'){
gpa += 2.0;
}
else if (grades[i] == 'D'){
gpa += 1.0;
}
else if (grades[i] == 'F'){
gpa += 0.0;
}
}

return gpa / grades.length;

}

public void ReadObject(ObjectInputStream in) throws IOException{
String readTitle = "";

for(int i = 0; i < name.length(); i++){
readTitle += in.readChar();
}
ssNum = in.readInt();
coursesCompleted = in.readInt();

for (int i = 0; i < grades.length; i++){
grades[i] = in.readChar();
}
}
public void WriteObjectOverride(ObjectOutputStream out) throws IOException, ClassNotFoundException{

out.writeChars(name);
out.writeInt(ssNum);
out.writeInt(coursesCompleted);

for (int i = 0; i < grades.length; i++){
out.writeChar(grades[i]);
}
gpa(grades);
}
public String toString(){
String student = name + " " + "\nGPA: " + gpa(grades);
return student;
}
}


public void ReadObject(ObjectInputStream in) throws IOException{
String readTitle = "";

for(int i = 0; i < name.length(); i++){
readTitle += in.readChar();
}
ssNum = in.readInt();
coursesCompleted = in.readInt();

for (int i = 0; i < grades.length; i++){
grades[i] = in.readChar();
}
}
public void WriteObjectOverride(ObjectOutputStream out) throws IOException, ClassNotFoundException{

out.writeChars(name);
out.writeInt(ssNum);
out.writeInt(coursesCompleted);

for (int i = 0; i < grades.length; i++){
out.writeChar(grades[i]);
}
gpa(grades);
}
public String toString(){
String student = name + " " + "\nGPA: " + gpa(grades);
return student;
}
}

然后我必须编写一个单独的程序将对象写入文件。我创建了一个构造函数,以便它给出名称、ss 编号和类(class)完成值,然后我使用 setter 方法来设置成绩数组值。然后,我使用了必须使用 writeObject 的值并编写了如下对象:

            Student john = new Student("John Doe", 123456, 4);
Student jane = new Student("Jane Doe", 987654, 3);
Student jack = new Student("Jack Doe", 528491, 4);

ObjectOutputStream out;

john.setGrades(student1);
jane.setGrades(student2);
jack.setGrades(student3);

try {
out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("students.bin")));

out.writeObject(john);
out.writeObject(jane);
out.writeObject(jack);

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

}

我遇到的问题是,当我使用另一个程序执行 readObject() 方法时,如下所示:

    public static void main (String[] args)throws FileNotFoundException, IOException, ClassNotFoundException{
Student student = null;
try(ObjectInputStream in = new ObjectInputStream(new FileInputStream("students.bin"));){

student = (Student)in.readObject();
System.out.println(student);
}

它打印出一行不是我预期的内容:

UnitSeven.Student@70dea4e

有谁知道如何解决这个问题,以便它显示实际的对象?任何帮助将不胜感激。

最佳答案

除了 Kumar Abhinav 指出的错误名称之外,您还存在几个问题。

您的readObject正在使用names.lengthgrades.length,但两者都尚未初始化。您可能需要修改 writeObject 来输出名称的长度,然后在读取名称之前读取该长度,或者只是将名称作为 String 对象写入和读取。您可以使用coursesCompleted来确定要阅读多少个等级。您也不用对 readTitle 变量执行任何操作。

您的 writeObject 方法调用 gpa 方法,但不使用返回值。

关于java - readObject() 方法不打印对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32025318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com