• Post Reply Bookmark Topic Watch Topic
  • New Topic

while iterator loop  RSS feed

 
Lee Dow
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

It may be easy, but I don�t know how to do the following:
I've got a number (also can be a string, there's no problem in converting it) inside a while(iterator.hasNext()) loop.

This number represents months, for example, in each iteration the while can throw a 12 which means december, a 7 which means july and so on ...

What I don't know how to do, is to catch the number throwed in each iteration in order to do something like: if number=12 print "december", if number = 7 then print "july" and so on, until the while has nothing else
to throw.

Thanks a lot
 
Eric Fletcher
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the javadoc for Iterator, specifically the next() method.

Iterator

HTH,
E
 
Lee Dow
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the answer, but I am afraid I explain myself awfully !

What I have in first place is a Collection with strings representing months in no particular order:
Collection months = new ArrayList();
months.add("7");
months.add("12");
months.add("11"); ... and many more, no necessarily 12 entries, they can be repeated.

This must be a Collection for later EJB finder method.

Now, the while:

Iterator it = months.iterator();
while (it.hasNext())
{
String s = (String) it.next(); // can be changed later for int if necessary

and the thing I don�t know is:
what should I do here to catch the 7 in first iteration and print that it means july, then catch the 12 in second iteration and print that it means december ..and so on

}

Hope I explained myself this time, thanks a milion again!
 
Eric Fletcher
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, in that case, you are essentially talking about mapping values, i.e. "7" maps to "July", so I would probably use a HashMap with the value returned from the iterator as the key, and have it map to the value you are looking for.

I don't want to spoon-feed you too much here (or insult you if you already know ), so I'll leave the implementation of that up to you as an exercise. Check the javadoc for HashMap and give it a shot. If you still have trouble post again.

Cheers,
E
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric has already given a road(hash)map for a solution. I'd just like to mention just this: You may be a beginner & hence this post - don't use words like throw & catch when you are not referring to exceptions. Throw & catch have a specific meaning in java & using them in places where they don't have anything to do with exceptions will, well, throw the reader off. The while loop is not throwing a 7 or a 12 or whatever. The iterator's next() method returns the next object in the collection.
 
Farl Mann
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Eric, I'll try it by myself.. just needed some light on it. Thanks again
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!