I have in my program an object of a class. When I try to print it out I am getting "Address@33909752" - guessing it is a hash code. I also tried to override toString method, but still didnt help

Address address = new Address(city, country, houseNo, street, postCode);

Is there any way to print out the object correctly? I mean to display values?

Recommended Answers

All 7 Replies

I don't know exactly what's wrong without viewing MORE CODE, but it should be fairly straight-forward to point you in the right direction regaurdless. Here is an example that should be more or less correct and should steer you twoard building a tostring method that fits your needs. This looks like java, or C#, so there is an example of each.

//JAVA IMPLEMENTATION OF TOSTRING
@Override public String toString(){

    return "Your string";
    //Here's a better idea of what you will have to do, you have to 
    //  return the string representations of all member variables. 
    //  most programming languages coerce member variables to strings provided
    //  one of the variables in the chain is actually a string, '+' concatenates strings
    //  If you have integers or something consecutive, cast to string and enclose in parenthasees
    //return memberOne.toString() + memberTwo.toString() + memberThree.toString();
}

//C# IMPLEMENTATION OF TOSTRING
public override string ToString(){
    return "your string";
}

//you can also use StringBuilder's/StringBuffer's if you are accumulating
//  a lot of different strings, and you are concerned about the concatenation
//  overhead. (depends on language)

Also, remember to put spaces in the correct places otherwise the printout will not look legible at all.

It will work if you override toString correctly. Check that tyour method signature is correct, ie

   @Override
   public String toString() {
      return <some String expression>;
   }

It still does not work:

my code:

Address address = new Address(city, country, houseNo, street, postCode);
 CreditDetails creditDetails = new CreditDetails(cardHoldersName,startDate,expiryDate, cardType, securityCode,cardNumber);

 RegistrationDetails registrationDetails = new RegistrationDetails(name, surname, address, creditDetails); 




@Override
 public String toString(){
     StringBuilder result = new StringBuilder();
     result.append(this.getClass().getName());
     result.append("Name: "+ name);
     result.append("Name: "+ surname);
     result.append("Name: "+ address);
     result.append("Name: "+ creditDetails);
     return result.toString();

 } 

I am still receiving memory address and class name

That method lookd good Where did you put it?
Where is the code that tries to print and gives the wrong output?

this method is in my client class where i have all the objects..

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package busypointninoshka;
import java.util.Scanner;

/**
 *
 * @author Admin-PC
 */
public class Client {

 private String name;
 private String surname;
 private Address address;
 private CreditDetails creditDetails; 
 private String city;
 private String country;
 private int houseNo;
 private String street;
 private String postCode;
 private String cardHoldersName;
 private String startDate;
 private String expiryDate;
 private String cardType;
 private short securityCode;
 private long cardNumber;
 private RegistrationDetails r;
 Scanner in = new Scanner(System.in);    

 public void setDetails(){
 System.out.println("Enter your name: ");
 name = in.nextLine();
 System.out.println("Enter your surname: ");
 surname = in.nextLine();
 System.out.println("Address");
 System.out.println("City: ");
 city = in.nextLine();
 System.out.println("Country: ");
 country = in.nextLine();
 System.out.println("House No: ");
 houseNo = in.nextInt();
 System.out.println("Street: ");
 street = in.nextLine();
 System.out.println("Post Code: ");
 postCode = in.nextLine();
 System.out.println("Credit Card Details");
 System.out.println("Card Holder's Name: ");
 cardHoldersName = in.nextLine();
 System.out.println("Start Date: ");
 startDate = in.nextLine();
 System.out.println("Expiry Date: ");
 expiryDate = in.nextLine();
 System.out.println("Card Type: ");
 cardType = in.nextLine();
 System.out.println("Security Code: ");
 securityCode = in.nextShort();
 System.out.println("Card Number: ");
 cardNumber = in.nextLong();

 address = new Address(city, country, houseNo, street, postCode);
 creditDetails = new CreditDetails(cardHoldersName,startDate,expiryDate, cardType, securityCode,cardNumber);

 RegistrationDetails registrationDetails = new RegistrationDetails(name, surname, address, creditDetails); 
System.out.println(registrationDetails);
}

 public void printRequest(){
     Request request = new Request();
     request.print(name);

 }

 @Override
 public String toString(){
     StringBuilder result = new StringBuilder();
     result.append(this.getClass().getName());
     result.append("Name: "+ name);
     result.append("Name: "+ surname);
     result.append("Name: "+ address);
     result.append("Name: "+ creditDetails);
     return result.toString();

 } 
}

And I am using println to print it out as it is shown in the code snippet

Your original problem is this: if you call a print statement like that, the compiler will automatically print the result of the toString method, as told by earlier posters.

If you don't override (specify for yourself) a toString method, it will take the toString method your class inherited from the Object class, which will print something as you get it.

If you add that toString method like you posted here, there are only two reasons I can think of you still get the same print:

  1. You've put the toString method in the wrong class. If you want to print out an instance of class Address, you have to add a toString method in your class Address. Since you have this in your toString: result.append("Name: "+ address); I assume you did it in your main class, but not in the classes this class is composed by.
  2. You did add the toString, but you didn't recompile the class before running.

My guess, it's the first. Make sure you have a good toString method in all your classes.

Yup
That toString() is in the Client class, but its an instance of one of the other classes that's being printed.
You need an appropriate toString() method in each and every one of your classses.
that you want to print.

commented: Yes, I see his problem now, that's exactly what is going on. +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.