Solved

Properties class cannot display default values

Posted on 2004-10-15
30
197 Views
Last Modified: 2013-11-23
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
Comment
Question by:ricjava
  • 16
  • 8
  • 6
30 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12317162
The default props are loaded in defaultProp, the others in newProp
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12317165
You will replace all the properties on your second load
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12317173
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12317181
Properties oldProps = new Properties();
// load them
Properties newProps = new Properties();
// load them

Properties combinedProps = new Properties();

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

0
 
LVL 37

Expert Comment

by:zzynx
ID: 12317184
or even:

      newProp.get("HomeTown", defaultProp.get("HomeTown"))
0
 

Author Comment

by:ricjava
ID: 12317192
If so, then what's the point of having constructor of

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

0
 
LVL 37

Expert Comment

by:zzynx
ID: 12317199
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
 

Author Comment

by:ricjava
ID: 12317210
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12317223
>>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
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 20 total points
ID: 12317242
Right. But I agree with ricjava, that it is strange that performing a load() deletes the defaults
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12317246
>>You can't.

Actually, i'm no longer certain, having seen that stuff ;-)
0
 

Author Comment

by:ricjava
ID: 12317247
yup zzynx...that's what i mean...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12317277
When I look at the code of the load() function I don't see any touching/clearing of the defaults...???
0
 

Author Comment

by:ricjava
ID: 12317322
I guess my program should work but unfortunately not.....

Should it be a flaw in java itself? (just joking)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12317326
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 37

Expert Comment

by:zzynx
ID: 12317328
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12317334
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12317343
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
 

Author Comment

by:ricjava
ID: 12317346
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12317347
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12317380
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12317414
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 80 total points
ID: 12317416
It's because get is not overridden to look in the defaults. It probably should be. Use getProperty
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12317446
OF COURSE!!! That's it.

You should use getProperty() [method of Properties class]
instead of get() [method of the HashTable class]
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12317455
That would be a nice one for some java exam ;°)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12317458
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12317460
That works, of course.
Money back guarantee. :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12336533
Thanks for appreciating our efforts to help
0
 

Author Comment

by:ricjava
ID: 12336758
u are welcome...
give ur students  a test on this, will u..?

:-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12341846
8-)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now