Change all string fields to uppercase

3.8k views Asked by At

We are using OpenCSV to map CSV file to Java Bean. I want to make all String values to uppar case while mapping it to Java bean.
Suppose CSV contains following line

abc, 123, def, 123abc   

while mapping it to bean it should be converted

ABC, 123, DEF, 123abc

I can go with reflection and have the desired result .But was looking for a solution through OpenCSV or Is there any feature in Apache Bean Utils or any other similar library which converts all String fields from a bean to uppercase?

2

There are 2 answers

1
Ravi K Thapliyal On BEST ANSWER

Use an overridden CsvToBean implementation:

CsvToBean<BeanType> csv2Bean = new CsvToBean<BeanType>() {

    @Override
    protected Object convertValue(String value, PropertyDescriptor prop) {
        return super.convertValue(value.toUpperCase(), prop);
    }
};

List<BeanType> beanList = csv2Bean.parse(mappingStrategy, csvReader);
1
newuser On

Try this, StringUtils.upperCase

if (StringUtils.isAlpha(value)) 
        {
            System.out.print("With uppercase " + StringUtils.upperCase(value));
        }
        else  // with number or anything with the word
        {
            System.out.print("Without uppercase " + value);
        }

or

System.out.println(""+(StringUtils.isAlpha(value) ? StringUtils.upperCase(value) : value));