to String value

97 Views Asked by At

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

3

There are 3 best solutions below

0
Kumar Abhinav On BEST ANSWER

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

6
lxcky On

Move your toString() implementation to your Uspsrates object.

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

Because your getRate() method returns a Uspsrates not Usps

0
SparkOn On

gives this output inship.Uspsrates@16672d6

what else do you expect it should give you

Your toString() is inside the Usps class but your getRate() method returns an Object of type Uspsrates

When you are doing this

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

It is just printing the hash of the object returned by getRate()

How to fix this?

Just add a toString() inside your Uspsrates class