1

I am still a newb, so please forgive my lack of knowledge. I have tried to save a string (manager name) which is taken as input via the scanner class. This works, I have also serialized the string, however when I de-serialize the string, the memory address is displayed, rather than the manager name.

How can I display the manager name, and from a learning point of view, why is my saved string defaulting to a memory address after serialization. Below are some code snippets, (Please don't mention try with resources, I am learning and will do this eventually!) thanks in advance :)

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

  • The manager name you are printing out is an object of type MainMenu which is probably why it's not printing out as a string. – mikea Jan 06 '16 at 15:25
  • 1
    Can you share your ClubInfo and MainMenu classes. Do you write these objects to the file? Do they implement serializable? – Eric G Jan 06 '16 at 15:25
  • Either get the manager name from MainMenu object or implement there proper toString() method. Also, not a good practice to save values into method parameter – Jan Hruby Jan 06 '16 at 15:37
  • @JanHruby ofcourse it contains a ToString() thats part of the object (https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html) But it isn't overriden by the ops MainMenu object. – Blaatz0r Jan 06 '16 at 15:39
  • @Blaatz0r do you honestly think that fact that each object in Java has default toString() method inherited from Object class was worth mentioning? – Jan Hruby Jan 06 '16 at 15:44
  • Actually I did. Since you had to edit your comment to change what you said. But no biggy. The op already mentioned that he is a newb.Not everyone knows that stuff upfront. – Blaatz0r Jan 06 '16 at 15:47
  • just seemed like know-it-all playing words, no offense. But even though I would be surprised if person capable of serializing/deserializing object would not be aware of it, as you said, it is possible – Jan Hruby Jan 06 '16 at 15:55
  • I looked at the ops track record and a lot of questions are about the same Project. I know serializing and deserializing examples are on SO by the bundle. But that doesn't make it so that he understands what is going on. :) – Blaatz0r Jan 06 '16 at 16:00

1 Answers1

1

Because the getFullName is an object MainMenu in this case. By default the ToString() in java is the mem adress. (Why does the default Object.toString() return a hex representation of the hashCode?)

System.out.println("Manager Name :" + getFullName);

If your MainMenu has a property or getter method then you should call that.

Eg. getFullName.GetManagerName()

from my point of view the name getFullName is poorly chosen. Since it doesn't contain a fullName it contains a MainMenu object. so if you change that to mainMenu it would be a better name.

Then you could have something like:

mainMenu.GetFullName() 

or

mainMenu.GetManagerName()
Community
  • 1
  • 1
Blaatz0r
  • 1,205
  • 1
  • 12
  • 24
  • I did have getters and setters, I have used the "getter" now and it worked first time. I agree about my naming of things, I find it hard to think of something more specific and meaningful. Anyway, thanks for your help :) – Darren Estcourt Jan 07 '16 at 15:22
  • I can relate to the naming issue Darren. In time it will become easier. Goodluck with your project :-) – Blaatz0r Jan 07 '16 at 15:25