• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Size of first Element of a Vector?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks to BJ Grau & Bosun Bello for my last post. But I have a new problem. I have created a "for loop" to print out my characters in the Vector "sentence". How do i define the size of the first element of that vector in my for loop? So far I have this...
for(int index = 0; index < sentence.size(); index++)
this defines the size of whole vector. i need to define the size of the first element only. Can anyone help??
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please elaborate on what you mean by finding the size of the first element of the Vector.
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure what you mean...I 'll take a stab though.
It all depends on what you stored in the first element of the vector. Whatever it is, hopefully it's something that you can ake a size of. If it's a string, cast it back to a string, and then check it's size. Do the same if it's an array, or another type of collection.
ex. (String) yourVector.elementAt(0)

Bosun

 
rikstar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay here it is. I have created a vector to hold a sentence that the user will type in. Each individual word has been placed in a different Vector element. Now what I need to do is to retrieve the first element of the Vector and print out it's contents onto the screen. However I will need to print each character of the string individually. Therefore I need a for loop which will use the size of the first element of the vector to exit from. How do i do this. So far I have....
for(int index = 0; index < sentence.size(); index
{
System.out.println(sentence.firstElement().toString().charAt(index));
}
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that you are making it harder than it needs to be:
String firstWord = (String)sentence.elementAt(0);
for(int index = 0; index < firstWord.length(); index++)
{
System.out.println(firstWord.charAt(index);
}
If you need to print every letter of every word, put another loop inside the one above and use the outer loop to cycle through the objects in the Vector and the inner to print individual letters.


------------------
Brian Hoff
Sun Certified Programmer for the Java� 2 Platform
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rikstar,
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at "JavaRanch Naming Policy" . We require names to have at least two words (not just letters), separated by a space, and strongly recommend that you use your full real name. We want to get to know you as a Professional. Please choose a new name which meets the requirements and re-register.
Cindy
 
BJ Grau
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rikstar - The problem with what your doing is you are iterating over the Vector sentence (no problem there), but then you are trying to reference the indiviual chars of your String with the same iterator. Here is a snippet of code that will iterate over each element in the Vector sentence, and then, assuming each is a String print out each char in each String.
Good luck with whatever it is your working on!
for(int index = 0; index < sentence.size(); index++)
{
String s = (String)sentence.elementAt(index);
for (int i = 0;i < s.length() ;i++ )
{
System.out.println(s.charAt(i));
}
}
 
rikstar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just wanted to say thanks to everyone who posted a reply to my topic, you guys and girls have been very helpful! Cheers!
P.S My Program is working like a well oiled machine! Thanks again!
 
reply
    Bookmark Topic Watch Topic
  • New Topic