This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori 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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting String Vector Elements??" Watch "Casting String Vector Elements??" New topic

Casting String Vector Elements??


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..
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.


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();
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);
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.

I agree. Here's the link:
subject: Casting String Vector Elements??
It's not a secret anymore!