Newbie: iterating system properties

Discussion in 'Java' started by roger_pearse@yahoo.co.uk, Mar 8, 2006.

  1. Guest

    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
     
    , Mar 8, 2006
    #1
    1. Advertising

  2. Guest

    try:

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

    }
     
    , Mar 8, 2006
    #2
    1. Advertising

  3. Guest

    Ignore that, i'm not reading properly!
     
    , Mar 8, 2006
    #3
  4. 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
    > 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---

    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);
    }

    --
    "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
     
    Thomas Fritsch, Mar 8, 2006
    #4
  5. Guest

    Thomas Fritsch wrote:
    > 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
    > > 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---

    > 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);
    > }


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

    All the best,

    Roger Pearse
     
    , Mar 8, 2006
    #5
  6. Roedy Green Guest

    Roedy Green, Mar 8, 2006
    #6
  7. Roedy Green Guest

    On 8 Mar 2006 11:54:40 -0800, wrote, quoted
    or indirectly quoted someone who said :

    > 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
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Mar 8, 2006
    #7
  8. Thomas Fritsch wrote:
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Mar 8, 2006
    #8
  9. Guest

    Roedy Green wrote:
    > On 8 Mar 2006 09:20:47 -0800, wrote, quoted
    > or indirectly quoted someone who said :
    >
    > >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
     
    , Mar 8, 2006
    #9
  10. Guest

    Thomas Fritsch wrote:
    > 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
     
    , Mar 8, 2006
    #10
  11. Guest

    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/
     
    , Mar 9, 2006
    #11
  12. Roedy Green Guest

    On 8 Mar 2006 15:30:30 -0800, wrote, quoted
    or indirectly quoted someone who said :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Mar 9, 2006
    #12
  13. Chris Uppal Guest

    wrote:

    > 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
     
    Chris Uppal, Mar 9, 2006
    #13
  14. Guest

    Chris Uppal wrote:
    > wrote:
    >
    > > 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 ;-)


    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
     
    , Mar 9, 2006
    #14
  15. <> wrote:
    > Chris Uppal wrote:

    [...]
    >> 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 ****()"
    --
    "TFritsch$t-online:de".replace(':','.').replace('$','@')
     
    Thomas Fritsch, Mar 9, 2006
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Coutts

    Newbie: iterating through folder contents

    Richard Coutts, Aug 30, 2003, in forum: C Programming
    Replies:
    1
    Views:
    326
    Richard Heathfield
    Aug 30, 2003
  2. Replies:
    4
    Views:
    468
    Steven Bethard
    Feb 17, 2005
  3. Andy Dingley

    [newbie] Iterating a list in reverse ?

    Andy Dingley, Jun 21, 2006, in forum: Python
    Replies:
    4
    Views:
    347
    Andy Dingley
    Jun 21, 2006
  4. Replies:
    5
    Views:
    356
    Michael Rauscher
    Oct 31, 2006
  5. carl
    Replies:
    5
    Views:
    2,427
    James Kanze
    Nov 25, 2009
Loading...

Share This Page