File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes classcastexception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "classcastexception" Watch "classcastexception" New topic
Author

classcastexception

pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
StringBuffer valueFirst =((StringBuffer)st.nextElement());

/***** i get a classcastexception in this line***********/

here

st /******** is a stringtokenizer object******************/

is it expecting a string?

the thing is i have to use stringbuffer for performence reasons
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
You should prefer the nextToken() method, it returns the same object as nextElement(), but makes it more visible that it returns a String.

You get the ClassCastException because you cannot cast from String to StringBuffer. If you really need a StringBuffer (e.g. to manipulate the string value), you will have to create a StringBuffer with the String you get from the nextToken() method.
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
String valueFirst=(StringBuffer)(st.nextToken());


/****** is it right************/
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
if i create a stringbuffer after i create a string

then what is the use , i mean string object has already been created and performance will degrade anyway,then what is the use of casting it to stringbuffer?
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
Where do you expect the performance gain/loss to come from?
The String is already created by the StringTokenizer. You don't get to choose there. In fact, depending on what you want to do, a String might even be better suited than a StringBuffer.


Now what are you planning to do with this String?
Adam Schaible
Ranch Hand

Joined: Oct 04, 2007
Posts: 101
Pre-optimization??

You can create a String in a few microseconds.

Whoever told you that creating a single string degrades performance doesn't quite grasp the big picture concept.

As someone has said in this thread - nextElement() gives you a String, no matter WHAT you do with it after you call the method.

If you're going to do a lot of string concatenation ("a" + "b" + "c") within loops, you may consider re-writing using StringBuffer (or StringBuilder) - consider though, the 1.5 compiler will do most of this for you.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

it seems to me that worrying about optimization at this point is pre-mature. You would do better to concentrate on writing clean, clear and understandable code.

THEN, if there are performance issues, worry about optimization. Even then, you need to use a profiler to determine where the slowdowns are. the odds are strongly against the slowdown being where you think it is. more often than not, people who guess where the slowdown is guess wrong, and spend weeks making changes to get incremental performance changes.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Adam Schaible:

You can create a String in a few tens of nanoseconds.


There, fixed that for you.


[Jess in Action][AskingGoodQuestions]
Adam Schaible
Ranch Hand

Joined: Oct 04, 2007
Posts: 101
Originally posted by Ernest Friedman-Hill:


There, fixed that for you.


Maybe even quicker!!!

Point being, don't stress over creating a String
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
while(st.hasMoreTokens())
{

String valueFirst = st.nextToken();
if ( !(vtokens.contains ( valueFirst.trim() ) ) ) {
vtokens.add(valueFirst);
}

}


/********* this is the entire loop***************/
i want stringbuffer because string will create many objects
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
You get the ClassCastException because you cannot cast from String to StringBuffer. If you really need a StringBuffer (e.g. to manipulate the string value), you will have to create a StringBuffer with the String you get from the nextToken() method.


how to create a StringBuffer with the value i get from nextToken method?
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234

If this is your loop, there is no point using a StringBuffer at all.
What is the vtokens? It looks like a Collection. If the order is not important, you can use a Set and then you do not have to check for the contains, a Set will not allow duplicates by nature.

If you really want a StringBuffer (I really think you don't need it here), you can do:
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
public class MultipleValueReduce extends DataFormatter {

public Object format(Object valueToBeFormated)
{
String value = null;

if(valueToBeFormated instanceof StringBuffer)
{
value = ((StringBuffer) valueToBeFormated).toString();

}
if(value == null || value.length() == 0)
{
System.out.println("error");
}
StringTokenizer st = new StringTokenizer(value, ",",
false);
int num = st.countTokens();
Vector vtokens = new Vector ();
if ( num>1 ) {
while(st.hasMoreTokens())
{
String valueTemp=st.nextToken();
if ( !(vtokens.contains ( valueTemp.trim()) ) ) {
vtokens.add(valueTemp);
}

}

int totalcount = vtokens.size();
for(int ncount = 0; ncount < totalcount; ncount++)
{
if(ncount == 0)
{
value = vtokens.get(ncount).toString();
}
else
{
value+=",";
value += vtokens.get(ncount).toString();
}
}

}
return value;
}

}
/*********** here is the entire program***************/
i have taken a lot of pain in writing this

the value i send in the method is

ABC,ABC,ABC
ABC,XYZ,ABC


the output i want is only unique values and duplicates has to be eliminated

can i Do this using a single StringBuffer object and without using vectors?

bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
Psst, your code is more readable when you use Code Tags.

Finally we get to see the rest of the code. The problem you mention is not in the tokenization but in the creation of the result string. It is there that you better use a StringBuffer (or a StringBuilder if you happen to use java 5).
Something like:


But you have some other issues in your code
- You check whether the trimmed version is in the Vector, but add an untrimmed version.
- You don't handle the situation where "num == 1" (I would not handle this as a special case).
- You will get a NullPointerException when the object is not a StringBuffer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: classcastexception