[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Properties class cannot display default values

Posted on 2004-10-15
30
Medium Priority
?
219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
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 80 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
 
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 320 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

656 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