我还是个新手,所以请原谅我缺乏知识。我试图保存一个字符串(管理器名称),它通过扫描仪类作为输入。这工作,我也序列化了字符串,但是当我反序列化字符串时,显示的是内存地址,而不是管理器名称。
如何显示管理器名称,从学习的角度来看,为什么我的保存字符串在序列化后默认为内存地址。下面是一些代码片段,(请不要提及尝试使用资源,我正在学习,最终会这样做!) 提前谢谢:)
import java.util.InputMismatchException;
import java.util.Scanner;
import java.io.*;
import java.util.Arrays;
public class ReadClub{
public void tryReadClub(ClubInfo club, MainMenu getFullName){
String fileName = "/home/cg/root/savedgames/" + club.clubID + " " + club.teamName;
try
{
FileInputStream fileIn = new FileInputStream("" + fileName + ".ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
club = (ClubInfo) in.readObject();
getFullName = (MainMenu) in.readObject();
in.close();
fileIn.close();
}catch(IOException i)
{
// i.printStackTrace();
System.out.println("Not a valid number");
return;
}catch(ClassNotFoundException c)
{
System.out.println("Club class not found");
c.printStackTrace();
return;
}
System.out.println("Saved game loaded...");
System.out.println("Name: " + club.teamName);
System.out.println("Stadium: " + club.stadium);
System.out.println("Division: " + club.division);
// System.out.println("SSN: " + club.SSN);
System.out.println("Stadium Capacity: " + club.stadiumCapacity);
System.out.println("Manager Name :" + getFullName);
}
}
public class DeSerializer extends ReadClub
{
public void DeSerialize(ClubInfo club){
MainMenu pass_choice2 = new MainMenu();
int passed_choice = pass_choice2.savedTeamList(club);
MainMenu getFullName = new MainMenu();
if(passed_choice==1){tryReadClub(club, getFullName);}
}
--------------------------------------------------------------------------------
// This method is held in my MainMenu class
public String createProfile(){
System.out.println("Please enter your first name: ");
Scanner in = new Scanner(System.in);
System.out.println("\n");
String firstName = in.nextLine();
System.out.println("Please enter your surname: ");
String surname = in.nextLine();
String fullName = firstName + surname;
System.out.println("Your full name is:" + fullName);
return fullName;
} // end createProfile
答案 0 :(得分:1)
因为在这种情况下getFullName是一个对象MainMenu。默认情况下,java中的ToString()是mem地址。 (Why does the default Object.toString() return a hex representation of the hashCode?)
System.out.println("Manager Name :" + getFullName);
如果你的MainMenu有一个属性或getter方法,那么你应该调用它。
EG。 getFullName.GetManagerName()
从我的角度来看,名称getFullName选择不当。由于它不包含fullName,因此它包含MainMenu对象。因此,如果您将其更改为mainMenu,那将是一个更好的名称。
然后你可以有类似的东西:
mainMenu.GetFullName()
或
mainMenu.GetManagerName()