Code from Core Java2 fails

P

printdude1968

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?
 
J

James McGill

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

enum is a reserved word now. Tell your javac "-source 1.3" and it
should build ok.
 
R

Roedy Green

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top