Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with Casting  RSS feed

 
cesar aviles
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

This question is most likely very entry level to most of you, but I'm new to programming. Please bare with me. I have an application that passes data from an Array into a Vector, a LinkedList and an ArrayList. Iam having problems display the data in the Collections. Instead of seeing the values in the collections I see what looks like some type of reference ID. Can Someone assist me on this. I would really appreiciate it. I have posted the code for both my classes below.

//Class 1

/*
* Cup.java
*
* Created on January 5, 2008, 10:12 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

/**
*
* @author Cesar
*/



public class Cup {

private String cupSize; //cupSize will be set to "small" or "large" for ounces in the constructor

private String cupMaterial; // will be set to "plastic" or "paper" in the constructor
private int sizeCode;

public static final int LARGE = 1;
public static final int SMALL = 0;


/** 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;
}

}//end of class


//class 2

/*
* CupTester.java
*
* Created on January 5, 2008, 11:03 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

/**
*
* @author Cesar
*/
import java.util.List;
import java.util.ArrayList;
import java.util.Vector;
import java.util.LinkedList;


public class CupTester {


//instantiating all objects that will be used
private static Cup largeCups = new Cup("large", "paper");
private static Cup smallCups = new Cup("small", "plastic");
LinkedList lList = new LinkedList();
ArrayList arrList = new ArrayList();
Vector vList = new Vector();



static final Object[] CupsArray =
{smallCups, smallCups, smallCups, smallCups,
largeCups, largeCups, largeCups, largeCups, largeCups};



public CupTester() {

for(Object cups : CupsArray)

lList.add(cups);


for(Object cups2 : CupsArray)


arrList.add(cups2);

for(Object cups3 : CupsArray)


vList.add(cups3);



}

public void PrintCups()
{
Cup s1 = (Cup) lList.get(0);

for(int i=0; i<CupsArray.length; i++)






System.out.printf("%s", lList.get(i));
//I've tried casting seem to give the same output.
System.out.println( "\n" + s1);




//System.out.printf("%s", arrList.get(1));
//System.out.printf("%s", vList.get(1));


}

public static void main(String [] args)
{
CupTester cp = new CupTester();
cp.PrintCups();
}




}
 
cesar aviles
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read another posting someone to use the toString() method.

I tried:

arrList.toString();

for( Object o : arrList)

System.out.println(o.toString());

That did not work for either. It gave me the same output.
 
prateek chaudhari
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For displaying the object state specific information, you need to override the toString() method.
In your case your Cup class can have a method public String toString(){ return <some String that shows objects state>;}

The output you are getting is because when you call System.out on any object the method calls the Objects toString() method, as the Object class default behavior shows the Hashcode you are getting that output.

so just override the toString() method and you will get the output the way you want it.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to create your own toString() method in the Cup class:

Or something similar.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Code Tags
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!