why wont this print good info?

Can someone explain to me why this output doesn't print correctly? Is it because I am crossing classes or something?

Output:

Cup@82ba41Cup@923e30Cup@130c19bCup@1f6a7b9Cup@7d772eCup@11b86e7Cup@35ce36Cup@757aefCup@d9f9c3Cup@9cab16

BUILD SUCCESSFUL (total time: 0 seconds)

/*

* Cup.java

*

* Created on May 23, 2007, 10:12 AM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

/**

*

* @author Pete Berardi

*/

publicclass Cup{

private String cupSize;

private String cupMaterial;

publicstaticfinalint LARGE = 1;

publicstaticfinalint SMALL = 0;

publicstaticfinalint PAPER = 2;

publicstaticfinalint PLASTIC =3;

privateint sizeCode;

privateint materialCode;

/** Creates a new instance of Cup */

public Cup(String cupSize, String cupMaterial){

this. cupSize = cupSize;

this.cupMaterial = cupMaterial;

if(cupSize.equals("large"))

sizeCode = LARGE;

else

sizeCode = SMALL;

if(cupMaterial.equals("plastic"))

materialCode = PLASTIC;

else materialCode = PAPER;

}

}

[2478 byte] By [pberardi1a] at [2007-11-27 5:18:43]
# 1
Override the toString method in you Cup class.
floundera at 2007-7-12 10:41:53 > top of Java-index,Java Essentials,New To Java...
# 2
...as you've been told before...
paulcwa at 2007-7-12 10:41:53 > top of Java-index,Java Essentials,New To Java...
# 3
> why this output doesn't print correctlyBTW it does print correctly. Java cannot read your mind to know what format you expect the ouput to be in. So if you don't tell it, it uses the default format.
floundera at 2007-7-12 10:41:53 > top of Java-index,Java Essentials,New To Java...
# 4
So, you're saying that Java has a bug in its telepathy module?
paulcwa at 2007-7-12 10:41:53 > top of Java-index,Java Essentials,New To Java...
# 5
Thanks flounder...i googled override string and now i understand that it is printing the JVM representation of the String. I just need to return the string correct?
pberardi1a at 2007-7-12 10:41:53 > top of Java-index,Java Essentials,New To Java...
# 6
Nah, probably missing a pesky import statement.
floundera at 2007-7-12 10:41:53 > top of Java-index,Java Essentials,New To Java...
# 7

> Thanks flounder...i googled override string and now i

> understand that it is printing the JVM representation

> of the String.

Since all classes extend Object either directly or indirectly they inherit the toString method of Object. So if you don't write your own, this is the method it will use.

> I just need to return the string correct?

Which String?

floundera at 2007-7-12 10:41:53 > top of Java-index,Java Essentials,New To Java...
# 8
I thought you didnt need import statements for String
pberardi1a at 2007-7-12 10:41:53 > top of Java-index,Java Essentials,New To Java...
# 9

something like this gives me decent output

public String toString()

{

return"Cup Size: "+cupSize+"\n"+

"Size Code: "+sizeCode+"\n"+

"Cup Material : "+cupMaterial+"\n"+

"Material Code : "+materialCode+"\n";

}

pberardi1a at 2007-7-12 10:41:53 > top of Java-index,Java Essentials,New To Java...
# 10
pberardi1,[url= http://forum.java.sun.com/rewards.jspa?threadID=5175923&doActions=assigntrue&dukes=10]Read this.[/url][url= http://forum.java.sun.com/rewards.jspa?threadID=5175927&doActions=rewardtrue&messageID=9679855&dukes=10]Then this.[/url]
CaptainMorgan08a at 2007-7-12 10:41:53 > top of Java-index,Java Essentials,New To Java...
# 11
How many duke stars do you all want?
pberardi1a at 2007-7-12 10:41:53 > top of Java-index,Java Essentials,New To Java...
# 12
> How many duke stars do you all want?Click the second link I gave you in reply 10 first!
CaptainMorgan08a at 2007-7-12 10:41:53 > top of Java-index,Java Essentials,New To Java...
# 13
> I thought you didnt need import statements for StringYou don't; he was making a counter-joke to my joke.
paulcwa at 2007-7-12 10:41:53 > top of Java-index,Java Essentials,New To Java...
# 14

but i am the author of this thread....anyway i have one more question...

what is it called when i put multiple strings into one address such as (see code)

I thought I would only be able to put "Small" into one address

How is it able to do this without a multidimensional array?

cupArray[0] = new Cup("Small", "Plastic");

Message was edited by:

pberardi1

pberardi1a at 2007-7-12 10:41:53 > top of Java-index,Java Essentials,New To Java...
# 15
You are storing the two strings in your Cup object and this object is what is stored in the array. Actually the references are stored not the objects.
floundera at 2007-7-21 21:25:02 > top of Java-index,Java Essentials,New To Java...
# 16

> something like this gives me decent output

It seems fine. Personally, I like to make my toString methods return something with a clear set of delimiters, so it'll be easier to read when it's among a lot of other output. Something more like this:

public String toString() {

return "{ Foo object: name: " + name + "; element: " + water + " }";

}

Also I prefer it if it's on a single line. But I can't say that's anything other than a personal preference.

Though now that I think about it, I may start standardizing all my toString output to be valid JSON strings. Seems like a handy practice...

paulcwa at 2007-7-21 21:25:02 > top of Java-index,Java Essentials,New To Java...