Solved

# Help needed for Properties class

Posted on 2005-04-05
Medium Priority
164 Views
Last Modified: 2013-11-23
Hi Experts ,
I have some string arrays ike

private static String[] Measuringpoints = {
"Date", "Time", "",
"E2-1N", "E2-2N", "E2-3N", "E2-4-1N", "E2-4-2", "E2-5-1",
"E2-5-2N", "E2-6-1N", "E2-6-2N", "E2-1NT", "E2-2NT", "E2-3NT",
"E2-4-1NT", "E2-4-2T", "E2-5-1T", "E2-5-2NT", "E2-6-1NT", "E2-6-2NT",
"E2-1NTK", "E2-2NTK", "E2-3NTK", "E2-4-1NTK", "E2-4-2TK", "E2-5-1TK",
"E2-5-2NTK", "E2-6-1NTK", "E2-6-2NTK"};

private static String[] Units = {
"", "", "",
"bar", "bar", "bar", "bar", "bar", "bar",
"bar", "bar", "bar", "°C", "°C", "°C",
"°C", "°C", "°C", "°C", "°C", "°C",
"bar", "bar", "bar", "bar", "bar", "bar",
"bar", "bar", "bar"};

public String fileneme="exp.txt";

I would like to add these strings to Properties p=new Properties() , and i have to use them in another class called exp.java.
Can you give me the code to this problem

Thanks
0
Question by:vihar123
• 10
• 6
• 5
21 Comments

LVL 92

Expert Comment

ID: 13712656

use a for loop to loop thru your arrays

for (int i=0; i<Units.length; i++)

and for each loop iteration add next element to properties file

p.putProperty(MeasuringPoints[i], Units[i]);
0

Author Comment

ID: 13712810
Can you give me the full code please?

Thanks
0

LVL 92

Expert Comment

ID: 13712855
sorry we aren't permitted to do that at EE
0

Author Comment

ID: 13713094
Ok ,

public static void main(String [] args){
Properties  fn   = new Properties();
Properties  meas = new Properties();
Properties  uts  = new Properties();
Set measures;
Set unts;
String str1;
String str2;

fn.put("filename","exp.txt");
for (int i=0; i<Measuringpoints.length; i++){
meas.put("Measuringpoints",Measuringpoints[i]);}
for (int i=0; i<Units.length; i++){
uts.put("Units :",Units[i]);}

measures=meas.keySet();
unts=uts.keySet();

Iterator itr1=measures.iterator();
while(itr1.hasNext()){
str1=(String) itr1.next();
System.out.println(" meas are :"+meas.getProperty(str1));
}

Iterator itr2=unts.iterator();
while(itr2.hasNext()){
str2=(String) itr2.next();
System.out.println(" unts are :"+uts.getProperty(str2));
}
}

From the above code that i have written ,
> System.out.println(" meas are :"+meas.getProperty(str1));printing only the last value of the that string array
> System.out.println(" unts are :"+uts.getProperty(str2)); printing only the last value of the that string array

Whats the mistake in my code?

0

LVL 15

Expert Comment

ID: 13713437
Properties p=new Properties(); // You provided this
for (int i=0; i<Units.length; i++) //provided above
p.setProperty(MeasuringPoints[i], Units[i]); // provided above (just changed putProperty to setProperty)

Then pass the the object p to the class you need.
(e.g.)
otherObject.setProperties(p); // have a setter in the other object
or new OtherObject(p)  // have the other object constructor get the properties
0

LVL 92

Expert Comment

ID: 13713918
What is it exactly you want map in your properties?
0

Author Comment

ID: 13726165
>> p.setProperty(MeasuringPoints[i], Units[i]);
Measuringpoints and Units are different , I would like to add them individually.

0

LVL 15

Expert Comment

ID: 13728607
Properties are based on key -> value pairs.
What do you mean by "I would like to add them individually"?
There are other data structurs like List which holds a chain of objects (one dimensional).
So if you want to different Lists individually you can do:
List measuringPointsList = java.util.Arrays.asList(MeasuringPoints);
List unitsList = java.util.Arrays.asList(Units);
and then pass to exp both lists (measuringPointsList and unitsList)
0

Author Comment

ID: 13739906
>> pass to exp both lists (measuringPointsList and unitsList)
How to pass these lists and how can i access them in exp class ?
0

LVL 15

Expert Comment

ID: 13741779
You need to have a reference to your exp class.
The exp class should have some public method that accepts two LisLists (the same concept applies to Properties or any other object)
(e.g:

List m_measuringPointsList;
List m_unitsList
public void setLists(List measuringPointsList, List unitsList)
{
m_measuringPointsList =  measuringPointsList;
m_unitsList = unitsList;
}

)

Then you can call exp.setLists(measuringPointsList, unitsList); // exp is the varibale that holds an instance of exp class
which will make those two lists available in exp via m_measuringPointsList and m_unitsList member varaibles).

For more information you can look at:
http://www.javaworld.com/javaworld/jw-03-2000/jw-0331-java101_p.html
0

Author Comment

ID: 13753987
Instead of Lists , i would like to use Properties class by using setProperty() and store()  Methods .
0

LVL 15

Expert Comment

ID: 13756358
Properties class is a dictionary type which means each item you add to it is associated with uniq key.
What are the key and value in your case?
0

Author Comment

ID: 13757322
is it possible to add  strings like
meas = E2-1N,E2-2N, E2-3N, E2-4-1N,E2-4-2, E2-5-1;
units = bar,bar, bar, bar, bar, bar, bar, bar;
Adding these two to property.setProperty(); method and then property.store()
0

LVL 92

Expert Comment

ID: 13757578
Yes that could be done, just build up a string containing the property value and call setProperty() to add it.
0

Author Comment

ID: 13757646
How to build up a string and setProperty ?
0

LVL 92

Accepted Solution

objects earned 500 total points
ID: 13757657
StringBuffer s = new StringBuffer();
for (int i=0; i<Units.length; i++)
{
s.append(Units[i]);
if (i<Units.length-1) s.append(", "):
}
props.setProperty("units", s.toString());
0

Author Comment

ID: 13757988
Hi ,
In exp.properties file , "°C" from Units is stored as " \u00B0C " .How to get the Correct value ?
0

LVL 15

Expert Comment

ID: 13758617
If you load exp.properties via the load method of the Properties class then such a translation (from unicode expression) to the valid
character will be done automatically (see: http://www.docjar.org/docs/api/java/util/Properties.html#load(java.io.InputStream) )
0

Author Comment

ID: 13759703
Why we are using " StringBuffer " only in the above code, is there any specific reason ?
0

LVL 92

Expert Comment

ID: 13759732
> Why we are using " StringBuffer " only in the above code, is there any specific reason ?

Just a bit more efficient for doing string concatenation, the toString() call at the end converts it to a String.
0

Author Comment

ID: 13789443
Thanks objects
0

## Featured Post

Question has a verified solution.

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

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 the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
###### Suggested Courses
Course of the Month17 days, 1 hour left to enroll

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

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