• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

Properties class cannot display default values

My Properties class is instantiated with a default Properties class. It is suppose to use that default values if key is not found. It doesn't work...here is my files


import java.util.*;
import java.io.*;

public class PropertyResearch
{
      public static void main(String[] args)
      {
            try
            {
                  Properties defaultProp = new Properties();
                  FileInputStream defaultValues = new FileInputStream("connieDefault");

                  defaultProp.load(defaultValues);

                  Properties newProp = new Properties(defaultProp);

                  FileInputStream newValues = new FileInputStream("connieNew");
                  newProp.load(newValues);

                  System.out.println("The name is " + newProp.get("Name"));
                  System.out.println("The company is " + newProp.get("Company"));
                  System.out.println("The home town is " + newProp.get("HomeTown"));

                  
                  defaultValues.close();
                  newValues.close();
                  

            }
            catch (Exception e)
            {
                  e.printStackTrace();
            }
            
            
            
      }
}

Here is the content of connieDefault file

Name = Connie
Occupation = Secret
HomeTown = Kampar
Company = Hotel Depalma
Marital Status = Single

Here is the content of connieNew file

Name = Connie
Occupation = Waitress
Company = Megami Club Depalma
Marital Status = Single
Hobby = Listening to MP3

When run, this is the output:

The name is Connie
The company name is Megami Club Depalma
The home town is null

Well, since the HomeTown is not found among the key, then it should used the default...why can't it use the default to print "The home town is Kampar"

Thanks


0
ricjava
Asked:
ricjava
  • 16
  • 8
  • 6
2 Solutions
 
zzynxSoftware engineerCommented:
The default props are loaded in defaultProp, the others in newProp
0
 
CEHJCommented:
You will replace all the properties on your second load
0
 
zzynxSoftware engineerCommented:
So, change

               System.out.println("The home town is " + newProp.get("HomeTown"));

into

               System.out.println("The home town is " + (newProp.get("HomeTown")==null) ? defaultProp.get("HomeTown") : newProp.get("HomeTown") );
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
Properties oldProps = new Properties();
// load them
Properties newProps = new Properties();
// load them

Properties combinedProps = new Properties();

combinedProps.putAll(oldProps);
combinedProps.putAll(newProps);

0
 
zzynxSoftware engineerCommented:
or even:

      newProp.get("HomeTown", defaultProp.get("HomeTown"))
0
 
ricjavaAuthor Commented:
If so, then what's the point of having constructor of

Properties(Properties defaults)
          Creates an empty property list with the specified defaults.

0
 
zzynxSoftware engineerCommented:
So replace

               System.out.println("The name is " + newProp.get("Name"));
               System.out.println("The company is " + newProp.get("Company"));
               System.out.println("The home town is " + newProp.get("HomeTown"));

by

               System.out.println("The name is " + newProp.get("Name", defaultProp.get("Name")));
               System.out.println("The company is " + newProp.get("Company", defaultProp.get("Company")));
               System.out.println("The home town is " + newProp.get("HomeTown", defaultProp.get("HomeTown")));
0
 
ricjavaAuthor Commented:
Let's look at http://java.sun.com/docs/books/tutorial/essential/attributes/properties.html

In particular this part -

Setting Up Your Properties Object
The following Java code performs the first two steps described in the previous section: loading the default properties and loading the remembered properties:
. . .
// create and load default properties
Properties defaultProps = new Properties();
FileInputStream in = new FileInputStream("defaultProperties");
defaultProps.load(in);
in.close();

// create program properties with default
Properties applicationProps = new Properties(defaultProps);

// now load properties from last invocation
in = new FileInputStream("appProperties");
applicationProps.load(in);
in.close();
. . .

First, the application sets up a default Properties object. This object contains the set of properties to use if values are not explicitly set elsewhere. Then the load method reads the default values from a file on disk named defaultProperties.
Next, the application uses a different constructor to create a second Properties object, applicationProps, whose default values are contained in defaultProps. The defaults come into play when a property is being retrieved. If the property can't be found in applicationProps, then its default list is searched.

Finally, the code loads a set of properties into applicationProps from a file named appProperties. The properties in this file are those that were saved from the program the last time it was invoked (the next section shows you how this was done).

0
 
CEHJCommented:
>>If so, then what's the point of having constructor of

...

that's fine. But you're expecting to somehow be able to combine them by loading more after doing that. You can't. You need to add them as i showed. You will then have both sets of properties. Properties with the same key in the 'old properties' will be replaced on the 'new properties' addition
0
 
zzynxSoftware engineerCommented:
Right. But I agree with ricjava, that it is strange that performing a load() deletes the defaults
0
 
CEHJCommented:
>>You can't.

Actually, i'm no longer certain, having seen that stuff ;-)
0
 
ricjavaAuthor Commented:
yup zzynx...that's what i mean...
0
 
zzynxSoftware engineerCommented:
When I look at the code of the load() function I don't see any touching/clearing of the defaults...???
0
 
ricjavaAuthor Commented:
I guess my program should work but unfortunately not.....

Should it be a flaw in java itself? (just joking)
0
 
CEHJCommented:
Using my example earlier, this has the desired result:

import java.util.*;
import java.io.*;

public class PropertyResearch
{
    public static void main(String[] args)
     {
         try
         {
              Properties defaultProp = new Properties();
              FileInputStream defaultValues = new FileInputStream("connieDefault");

              defaultProp.load(defaultValues);

              //Properties newProp = new Properties(defaultProp);
              Properties newProp = new Properties();

              FileInputStream newValues = new FileInputStream("connieNew");
              newProp.load(newValues);
              newProp.putAll(defaultProp);


              System.out.println("The name is " + newProp.get("Name"));
              System.out.println("The company is " + newProp.get("Company"));
              System.out.println("The home town is " + newProp.get("HomeTown"));


               defaultValues.close();
              newValues.close();


          }
         catch (Exception e)
         {
              e.printStackTrace();
         }



    }
}
0
 
zzynxSoftware engineerCommented:
It's not the load() that causes the problem.

If I have

               defaultProp.load(defaultValues);
               System.out.println(defaultProp.size());          // This prints out 1 (in my test case)

               Properties newProp = new Properties(defaultProp);
               System.out.println(newProp.size());                               // <<< ... while this prints out 0 !!!!!! ????

???
0
 
zzynxSoftware engineerCommented:
CEHJ, we know you can have that workaround. The Q is why is it needed?

PS. Sure your example is right? I think the default will overwrite the new ones in your example...
0
 
CEHJCommented:
The default thing is just that. It's for providing defaults. If they're not there in the new props, they'll be ignored
0
 
ricjavaAuthor Commented:
You program may work

But according to a tutorial in sun's website given earlier...my program should work...otherwise what is the purpose of the second constructor of Properties

Properties(Properties defaults)
          Creates an empty property list with the specified defaults
0
 
zzynxSoftware engineerCommented:
Think should be              

              Properties newProp = new Properties();
              FileInputStream defaultValues = new FileInputStream("connieDefault");
              newProp.load(defaultValues);

              Properties Prop2 = new Properties();
              FileInputStream newValues = new FileInputStream("connieNew");
              Prop2.load(newValues);
              newProp.putAll(Prop2);
0
 
zzynxSoftware engineerCommented:
1) You load the defaults
2) You overwrite the defaults with the new ones

It's a mystery why the constructor with the Properties parameter doesn't seem to work...
0
 
zzynxSoftware engineerCommented:
Forget this remark:

               defaultProp.load(defaultValues);
               System.out.println(defaultProp.size());          // This prints out 1 (in my test case)

               Properties newProp = new Properties(defaultProp);
               System.out.println(newProp.size());                              

The last line prints 0 correctly, since the hashtable is indeed empty (the defaults are kept in another HashTable):


class Properties extends Hashtable {

        protected Properties defaults;

        ...

        public Properties(Properties defaults) {
              this.defaults = defaults;
        }

}

But the main questions stays...
0
 
CEHJCommented:
It's because get is not overridden to look in the defaults. It probably should be. Use getProperty
0
 
zzynxSoftware engineerCommented:
OF COURSE!!! That's it.

You should use getProperty() [method of Properties class]
instead of get() [method of the HashTable class]
0
 
zzynxSoftware engineerCommented:
That would be a nice one for some java exam ;°)
0
 
zzynxSoftware engineerCommented:
So, ricjava:

Replace

               System.out.println("The name is " + newProp.get("Name"));
               System.out.println("The company is " + newProp.get("Company"));
               System.out.println("The home town is " + newProp.get("HomeTown"));

by

               System.out.println("The name is " + newProp.getProperty("Name"));
               System.out.println("The company is " + newProp.getProperty("Company"));
               System.out.println("The home town is " + newProp.getProperty("HomeTown"));
0
 
zzynxSoftware engineerCommented:
That works, of course.
Money back guarantee. :)
0
 
zzynxSoftware engineerCommented:
Thanks for appreciating our efforts to help
0
 
ricjavaAuthor Commented:
u are welcome...
give ur students  a test on this, will u..?

:-)
0
 
CEHJCommented:
8-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 16
  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now