Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Enumeration infinitely looping

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
I am retrieving values from a properties file, stored as key=value pairs. This is happening okay and I can put the vlaues into a properties object. By calling the size method on the obect I get the value one, as expected.
Now I need to find out how many key=value pairs there are in the file, and to do this I am enumerating over the contents of the properties object.
However, and this is where the problem lies my enumerating falls into an infinite loop, even though there is only one entry in the file. The file is called smtp.properties and I wrote it in notepad and hit return after the first entry.
My code is:
/////////////////////////////////////////////////////////
Properties fileProps = new Properties();
try {
FileInputStream is = new FileInputStream(propertyFile);
fileProps.load(is);
int i = fileProps.size();
System.out.println("size = " + i);
}
catch (IOException ioe) {
System.out.println("File not found: " + ioe.getMessage());
}
Enumeration smtpHostsProps = fileProps.propertyNames();
int i = 0;
while( smtpHostsProps.hasMoreElements() ) {
**********************************************************
infinite loop occurs here!
**********************************************************
i++;
}
System.out.println("i = " + i);
///////////////////////////////////////////////////////////
any help is greatly appreciated.
T
 
le taylom
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How silly,
although writing it here was good as I worked it out!
I get the size by calling the size() method.
D'OH (in capitals, and a slap of the forehead!!!)
T
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's good that you realized that there was a simpler method of accomplishing your goal, but I think it would be educational to address why you had a problem with your original solution. I believe the problem with your code snippet is that you called the hasMoreElements() method, but you never called nextElement(). Thus, the collection always had one more element it could get. The hasMoreElements() method call has no more effect by itself than does the size() method.
 
le taylom
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, thanks for the reply. I see what you're saying.
I have now changed the design of the code entirely and gone for something a bit cleaner.
Instead of enumerating through several name=value pairs I am listing the IP addresses on one line (as one value), delimiting them with a ';' and then tokenizing the string and splitting each IP into a part of an array object.
The best thing is it all works. So far......now its just some unit testing to confirm it!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic