Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why does my code print out the same word 10 times

 
jason lunt
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have this in my code but it prints out the element what i want but it will print it 12 times which is how many elements are in the vector how do i get it to print it out only once?
for (Enumeration e = v.elements();e.hasMoreElements() {
String se = (String)e.nextElement();
String s1 = (String)v.get(1);

System.out.println(s1);
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have this line?
String s1 = (String)v.get(1);
Why are you printing s1 rather than se?
 
jason lunt
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i want it to print out the first element of the vector and that is how i thought i should do it any other suggestions woulud be most pleased thanks
jason
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, why are you using an Enumeration at all?
 
jason lunt
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could you please give me some ideas in what i should use
thanks
jason
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at The J2SE Documentation for the Vector class. You should note a couple of methods that you could use to get the first component of a Vector. Note that I've emphasized the word get and that the first component in a Vector is at index 0.
 
jason lunt
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers everyone for the ideas and that
i have found it now
and it works thanks alot
jason
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic