This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Casting String Vector Elements?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting String Vector Elements??" Watch "Casting String Vector Elements??" New topic
Author

Casting String Vector Elements??

rikstar
Greenhorn

Joined: Sep 02, 2001
Posts: 6
How do I cast a string which is the first Vector element into a char type? i.e I have a Vector which holds strings, and I need to retrieve it inorder to print onto the screen. BUT I need to print the each character individually, which means I have to cast the "string" into type "char" BUT HOW? So far I have this..
System.out.println(sentence.firstElement());
I need a solution ASAP
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
I believe there is s toCharArray method that you can use. I would personally loop through the string printing out each element of the string. Also check out tghe charAt method.

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
BJ Grau
Ranch Hand

Joined: Jul 10, 2001
Posts: 234
I agree with Bosun - I would do something like this:
Vector v = new Vector();
v.addElement("One");
v.addElement("");
v.addElement("Two");
v.addElement("Three");
Enumeration e = v.elements();
while(e.hasMoreElements()) {
String s = (String) e.nextElement();
for (int i = 0;i < s.length();i++) {
char c = s.charAt(i);
System.out.println(c);
}
}
Jason Ford
Ranch Hand

Joined: Aug 02, 2001
Posts: 54
Why do you need to print each character individually? Is it a requirement of your program? If not, you should just print out the entire string at once; your code will be easier to read and maintain.
Jason


SCJP, SCWCD
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Casting String Vector Elements??
 
Similar Threads
Converting Strings to ints and other impracticalities
vector that takes only String
Printing an Object without casting!
Size of first Element of a Vector?
Class Cast Exception - Please help