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]

> 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?
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";
}
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
> 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...