webcaching.guerrerotome.com

Just another WordPress weblog

Posted by Carlos Guerrero on November 23, 2009

Java: get the value of a parameter which name is in a string

I got this exemple to understand it:

import java.awt.Rectangle;
import java.lang.reflect.Field;

public class SampleGet {

public static void main(String[] args) {
Rectangle r = new Rectangle(100, 325);
printHeight(r);

}

static void printHeight(Rectangle r) {
Field heightField;
Integer heightValue;
Class c = r.getClass();
try {
heightField = c.getField(”height”);
heightValue = (Integer) heightField.get(r);
System.out.println(”Height: ” + heightValue.toString());
} catch (NoSuchFieldException e) {
System.out.println(e);
} catch (SecurityException e) {
System.out.println(e);
} catch (IllegalAccessException e) {
System.out.println(e);
}
}
}

And I finally get that:

private Double getStatsValue(Vector<Integer> v, String parameter){

java.lang.reflect.Field f;
double result =Double.MAX_VALUE;
try {

f = CharacterizationValues.class.getField(parameter);
double singleValue = 0.0;
double[] allValues = new double[v.size()];
//            System.out.println();
//            System.out.print(”@”);
for( int i=0; i<v.size() ; i ++ ){
Integer file = v.elementAt(i).intValue();
CharacterizationValues ch = elementsCharacterization.get(file);

singleValue = f.getDouble(ch);
//                System.out.print(singleValue+”;”);

allValues[i]=singleValue;
}
//            System.out.println();
result = StdStats.stddevP(allValues);

} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return result;

}

Add A Comment