Code from Core Java2 fails

Discussion in 'Java' started by printdude1968@gmail.com, May 10, 2006.

  1. Guest

    I'm running Java 5.0 using Eclipse.
    This piece of code is failing within the IDE.


    import java.util.*;

    /**
    This program prints out all system properties.
    */
    public class SystemInfo
    {
    public static void main(String args[])
    {
    Properties systemProperties = System.getProperties();
    Enumeration enum = systemProperties.propertyNames();
    while (enum.hasMoreElements())
    {
    String key = (String)enum.nextElement();
    System.out.println(key + "=" +
    systemProperties.getProperty(key));
    }
    }
    }

    The API says (I think) that it's right but nothing I do can make it
    work.
    Any ideas?
    , May 10, 2006
    #1
    1. Advertising

  2. James McGill Guest

    On Tue, 2006-05-09 at 17:20 -0700, wrote:

    > Enumeration enum = systemProperties.propertyNames();
    > while (enum.hasMoreElements())


    enum is a reserved word now. Tell your javac "-source 1.3" and it
    should build ok.
    James McGill, May 10, 2006
    #2
    1. Advertising

  3. Roedy Green Guest

    On 9 May 2006 17:20:23 -0700, ""
    <> wrote, quoted or indirectly quoted someone
    who said :

    >
    >Any ideas?


    here is my code from Wassup that does the same thing:

    /**
    * Get a sorted list of all the system properties. Only works in
    * applications and signed Applets.
    *
    * @param separator usually "\n\n"
    *
    * @return String contaning pairs of property-value
    */
    public static String displayAllProperties( String separator )
    {
    try
    {
    Properties sysprops = System.getProperties();

    // Count properties
    int count = sysprops.size();

    // prepare Matrix to hold the properties
    String[][] matrix = new String[ count ][ 2 ];

    // read System properties into the matrix
    int j = 0; // Java won't let me put this in the for loop,
    Ouch!
    for ( Enumeration e = sysprops.propertyNames(); j < count;
    j++ )
    {
    String key = (String) e.nextElement();
    String value = sysprops.getProperty( key );
    matrix[ j ][ 0 ] = key;
    matrix[ j ][ 1 ] = value;
    } // end for

    // sort by key
    Arrays.sort( matrix, new StringComparator() );

    // concatenate all key value pairs.
    StringBuffer result = new StringBuffer( 4096 );
    for ( int i = 0; i < count; i++ )
    {
    String key = matrix[ i ][ 0 ];
    if ( key != null )
    {
    String value = matrix[ i ][ 1 ];
    if ( value != null )
    {
    if ( value.equals( "\r\n" ) )
    {
    value = "[hex chars: 0x0d 0x0a i.e. CrLf,
    \\r\\n]";
    }
    else if ( value.equals( "\n" ) )
    {
    value = "[hex char: 0x0a i.e. Lf, \\n]";
    }
    else if ( value.equals( "\r" ) )
    {
    value = "[hex char: 0x0d i.e. Cr, \\r]";
    }
    result.append( key );
    result.append( " = " );
    result.append( value );
    result.append( separator );
    }
    }
    } // end for
    return result.toString();
    }
    catch ( Exception e )
    {
    return "No security clearance to see the restricted System
    properties.";
    }
    } // end displayAllProperties

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, May 10, 2006
    #3
    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. Replies:
    0
    Views:
    429
  2. Lionel Chin
    Replies:
    0
    Views:
    329
    Lionel Chin
    Aug 26, 2004
  3. Replies:
    0
    Views:
    331
  4. jalexa9898

    Question about Java2

    jalexa9898, Jul 6, 2006, in forum: Java
    Replies:
    6
    Views:
    413
    Oliver Wong
    Jul 11, 2006
  5. motokoit

    python under java2

    motokoit, Feb 7, 2005, in forum: Python
    Replies:
    1
    Views:
    704
    Steve Horsley
    Feb 7, 2005
Loading...

Share This Page