Search code examples
javaobject-to-string

to String value


Hello all I am new in java.Please help me

I have following code

package sun.mun.fun;

import inship.InShipException;
import inship.PackageDetail;
import inship.Uspsrates;
import inship.UspsratesAboutPropertyEditor;
import inship.UspsratesBeanInfo;
import inship.UspsratesRequestedServicePropertyEditor;

public class Usps {


    public Uspsrates getRate()  {
        Uspsrates rate = new Uspsrates();

        try {
            rate.getUSPSAccount().setServer(rate.toString());
            rate.getUSPSAccount().setServer("http://production.shippingapis.com/ShippingAPI.dll");  
            rate.getUSPSAccount().setUserId("747THEFI1034");
            rate.getUSPSAccount().setPassword("510QU40FX616");
            rate.getSenderAddress().setZipCode("27502");
            rate.getRecipientAddress().setZipCode("20770");
            rate.getPackages().add(new PackageDetail());
            rate.getPackages().item(0).setWeight("1");
            rate.getPackages().item(0).setLength(Integer.parseInt("5"));
            rate.getPackages().item(0).setWidth(Integer.parseInt("5"));
            rate.getPackages().item(0).setHeight(Integer.parseInt("5"));    
            rate.getPackages().item(0).setGirth((2 * rate.getPackages().item(0).getLength()) + (2 * rate.getPackages().item(0).getWidth()));
            rate.getPackages().item(0).setSize(Integer.parseInt("0"));
            rate.getPackages().item(0).setPackagingType(inship.PackageDetail.ptNone);
            rate.setRequestedService(inship.Uspsrates.stUnspecified);
            rate.getRates();

        } catch (InShipException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return rate;


    }


@Override
public String toString() {
    return "Usps [getRate()=" + getRate() + "]";
}



public static void main(String[] args) {

    Usps u = new Usps();

    System.out.println(u.getRate().toString());


}


}

and when I run this it gives this output

inship.Uspsrates@16672d6

I don't want this value.I want this in string.What should i do any help.

Thanks in advance


Solution

  • Uspsrates class should override toString() method and should return a String with the details you want.If you simply print an Object,the compiler converts it into Object.toString() and returns a String in the following format ClassName@Hashcode

    Check the documentation for the Object's to String method here