• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to cast array of Object  RSS feed

 
Roberto Dupon
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I wrote this function that returns array of string

Code ---------------------------------------------------------
public String[] tokenizer(String data) {
String delim = "_";
Vector token = new Vector();
StringTokenizer st = new StringTokenizer(data, delim);
while (st.hasMoreTokens()) {
token.addElement(st.nextToken());
}
(String[]) token.toArray();
}
--------------------------------------------------------------
the program compiles but returns null at the execution
what's wrong?
thanks.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roberto,
Welcome to JavaRanch!

That code shouldn't even compile as it is missing a return statement. The last line should be:


As a debugging aid, you could add

to see what is in the Vector.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no return statement but assuming you left that out you will get a runtime error casting an object array to a String array. This should work:

return (String[])token.toArray(new String[0]);

Notice toArray receives a String array. That tells it the type. As an added convenience it creates a new array if the one you give it cannot hold all of the elements in the Vector. I frequently pass in an anonymous zero-length array as shown.

There are other issues with that code. you should use ArrayList rather than Vector in most cases (Vector is synchronized thus slower). Also, assuming a recent version of Java, split is cleaner:

String[] values = input.split("_");
 
Rick Portugal
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...which could also be rendered as

except that that assumes JDK 5.0, and I'm not certain that a person using Vectors and StringTokenizers is even using a JDK version from this milennium. Which takes us to the pre-Tiger version:

By the way I'd agree with ROS that split() is probably preferable anyway. Though there are some subtle differences if it's possible to have two or more consecutive delimiters, and a knowledge of regular expressions is helpful for fully taking advantage of split().
 
Roberto Dupon
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Thank you all for you help.
Could anyone spell out how to use split() method.
Thank you.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rick's post showed exactly how to use split() - his variable "input" must be a String; I guess in your original code you called it "data".

You can find more examples here.
 
lade Robinson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guyz.
stumbled across this posts whilst browsing this morning and i must confess that it has being of great value to me. u guyz just saved me the stress of working thru some clumsy vectors and i'm grateful to u's all.

u neva know who may stumble on ur woteva trail u've left behind.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!