Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Newbie: iterating system properties

Thread view: 
roger_pearse@yahoo.co.uk - 08 Mar 2006 17:20 GMT
I'm sure this is a silly question, but I'd like to iterate through the
system properties, and can't figure out from the Sun tutorial how to do
this.  Modifying the simplest HelloWorldApp:

---start---
import java.util.*;

class HelloWorldApp {
   public static void main(String[] args) {
       System.out.println("Hello World!"); //Display the string.

       Properties myprops = System.getProperties();
       // need to loop around these somehow and print out...
       for (Iterator i = myprops.iterator(); myprops.hasNext(); ) {
       }
   }
}
---end---

This won't compile:

HelloWorldApp.java:13: cannot find symbol
symbol  : method iterator()
location: class java.util.Properties
               for (Iterator i = myprops.iterator();
myprops.hasNext(); ) {
                                        ^
HelloWorldApp.java:13: cannot find symbol
symbol  : method hasNext()
location: class java.util.Properties
               for (Iterator i = myprops.iterator();
myprops.hasNext(); ) {
                                                            ^
2 errors

No doubt I am doing something really crass -- anyone care to help me?

Thanks,

Roger Pearse
steve_marjoribanks@hotmail.com - 08 Mar 2006 17:23 GMT
try:

Iterator i = myprops.iterator();
while (myprops.hasNext())
{

}
steve_marjoribanks@hotmail.com - 08 Mar 2006 17:54 GMT
Ignore that, i'm not reading properly!
Thomas Fritsch - 08 Mar 2006 18:41 GMT
roger_pearse@yahoo.co.uk schrieb:
> I'm sure this is a silly question, but I'd like to iterate through the
> system properties, and can't figure out from the Sun tutorial how to do
[quoted text clipped - 14 lines]
> }
> ---end---
Unfortunately Properties doesn't implement the Iterable interface and
hence doesn't have the iterator() method.
Therefore you must do it the old-fashioned way, i.e. using Enumeration:

  for (Enumeration e = myprops.keys(); e.hasMoreElements(); /**/) {
      String key = (String) e.nextElement();
      String value = myprops.getProperty(key);
      System.out.println(key + " = " + value);
  }

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

roger_pearse@yahoo.co.uk - 08 Mar 2006 19:54 GMT
> roger_pearse@yahoo.co.uk schrieb:
> > I'm sure this is a silly question, but I'd like to iterate through the
[quoted text clipped - 24 lines]
>        System.out.println(key + " = " + value);
>    }

I am very grateful for the explanation, and the example.  I shall now
go and find out about Enumeration.

All the best,

Roger Pearse
Roedy Green - 08 Mar 2006 20:46 GMT
>  I shall now
>go and find out about Enumeration.

Don't get it confused with enums, the new JDK 1.5 feature.

See http://mindprod.com/jgloss/jcheat.html#LOOPS
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Thomas Hawtin - 08 Mar 2006 20:45 GMT
> Unfortunately Properties doesn't implement the Iterable interface and
> hence doesn't have the iterator() method.
> Therefore you must do it the old-fashioned way, i.e. using Enumeration:

You can do it the new (1.2) way. Properties implements Map. So you can
do something like:

         for (Map.Entry<Object, Object> entry : properties.entrySet()) {

Or if you just wanted keys:

         for (Object key : properties.keySet()) {

Or from 1.6:

         for (String key : properties.stringPropertyNames()) {

(The last two can produce a different set.)

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

roger_pearse@yahoo.co.uk - 08 Mar 2006 23:30 GMT
> Unfortunately Properties doesn't implement the Iterable interface and
> hence doesn't have the iterator() method.
> Therefore you must do it the old-fashioned way, i.e. using Enumeration:

I now have a further question, which again is probably daft, but
followed on when I tried to find out how I might have known this
information without appealing for it.

I went and searched for Enumeration, which I found to be an interface:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Enumeration.html

I saw the stuff about using the elements() method of the classes to get
an Enumeration object.  I remembered that Properties was a subclass of
Hashtable, and so it was.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Hashtable.html
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html

But when I looked at Properties, the interfaces implemented were only
Cloneable, Map and Serializable.  The same was true of Hashtable.  So
how should I have known that it implemented (or inherited an
implementation of) Enumeration?

I hope this is clear -- I'm trying to find how I should have got the
answer from the docs myself, for next time.

All the best,

Roger Pearse
Roedy Green - 09 Mar 2006 01:36 GMT
>But when I looked at Properties, the interfaces implemented were only
>Cloneable, Map and Serializable.  The same was true of Hashtable.  So
>how should I have known that it implemented (or inherited an
>implementation of) Enumeration?

With Iterators the collection implements Iterable which means the
Collection will spew out an Iterator on demand.  

With Enumerators there in no equivalent to Iterable. You just have to
dig around for the method that will give you the Enumeration.

Dig around and you will find Properties.elements which produces an
Enumeration.

For details see http://mindprod.com/jgloss/enumeration.html
and http://mindprod.com/jgloss/iterator.html

This brings to a peeve. for:each should support Iterators, not just
Iterables.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Chris Uppal - 09 Mar 2006 10:10 GMT
> I hope this is clear -- I'm trying to find how I should have got the
> answer from the docs myself, for next time.

Your difficulty is partly a symptom of bad design in Properties and (arguably)
Hashtable.  The information you were looking for is not in the usual places.

The only way that you could have found it is by the following algorithm.

(0) Start with the class you are interested in.

(1) Read the /whole/ of it's JavaDoc (class and method comments) to see if
there's anything which would solve your problem.  If you find anything then the
search terminates.

(2) Switch your attention to the immediate superclass.

(3) GOTO 1.

If you end up in Object with still no solution, then repeat the search but this
time branch outwards to check package JavaDoc, and the JavaDoc of any
implemented interfaces too.

Also check the listed subclasses of the class you started with.  It's possible
that you have been given an object which is actually an instance of a
documented subclass of the declared class.  It might be that the feature you
are looking for is only available on the subclass, and that you are expected to
use a cast to get access to the feature (HttpURLConnection, anyone ?).

I.e, not just RTFM, but RT/W/FM ;-)

   -- chris
roger_pearse@yahoo.co.uk - 09 Mar 2006 21:22 GMT
> > I hope this is clear -- I'm trying to find how I should have got the
> > answer from the docs myself, for next time.
[quoted text clipped - 25 lines]
>
> I.e, not just RTFM, but RT/W/FM ;-)

Well, I thought I did.  But it didn't indicate that Properties
implemented Enumeration.  Did I miss something? (This is a genuine
question).

All the best,

Roger Pearse
Thomas Fritsch - 09 Mar 2006 22:00 GMT
[...]
>> I.e, not just RTFM, but RT/W/FM ;-)
>
> Well, I thought I did.  But it didn't indicate that Properties
> implemented Enumeration.  Did I miss something? (This is a genuine
> question).
Yes, Properties does not implement the Enumeration interface.
But it has a method which returns an Enumeration.
 public Enumeration keys();

Therefore, you have to browse through the API docs
not only looking for "... implements Enumeration",
but also looking for methods declared as "public Enumeration ****()"
Signature

"TFritsch$t-online:de".replace(':','.').replace('$','@')

Roedy Green - 08 Mar 2006 20:45 GMT
>I'm sure this is a silly question, but I'd like to iterate through the
>system properties,

see source code in Wassup. http://mindprod.com/applets/wassup.html
also source code at http://mindprod.com/jgloss/properties.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

roger_pearse@yahoo.co.uk - 08 Mar 2006 23:25 GMT
> >I'm sure this is a silly question, but I'd like to iterate through the
> >system properties,
>
> see source code in Wassup. http://mindprod.com/applets/wassup.html
> also source code at http://mindprod.com/jgloss/properties.html

Thank you very much for all these links -- very useful indeed!

All the best,

Roger Pearse
christian.bongiorno@gmail.com - 09 Mar 2006 00:05 GMT
Hi there. If it's a Map it has  a key, a value and an entry  -- all
three you can iterate on

Map sortedProps = new TreeMap(System.getProperties());

for(Iterator i = sortedProps.entrySet().iterator(); i.hasNext(); )
  System.out.println(i.next());

This will actually sort the properties for you if you are looking for
one. I routinely use this in the debugger to look at a property where I

1) can't be bothered to type out the entry property name and fetch it,
or
2) can't remember the entire name/spell it correctly

Since you seem new enough, System.out.println calls the "toString()" on
any object passed in (null objects just print "null") -- so, rather
than even having to do the String formatting for the output you will
get {key,value}  or {key => value}. You could take it one step further
and just do System.out.println(System.getProperties())  and you would
get the AbstractMap implementation of toString()

Either way, it makes your life simpler

Christian
http://christian.bongiorno.org/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.