0

I am trying to read a CSV file having some integer values with spaces. There is no way i can get that file having integer values without spaces. Using bean io, when i am trying to read this file it gives a conversion error while reading integer values. Here is my bean

public class PetOwner {
    private String name;
    private int age;
    private int numberOfPets;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getNumberOfPets() {
        return numberOfPets;
    }
    public void setNumberOfPets(int numberOfPets) {
        this.numberOfPets = numberOfPets;
    }

}

Here is my mapping file

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<beanio xmlns="http://www.beanio.org/2012/03">
    <stream name="petowner" format="csv">
    <parser>
        <property name="delimiter" value="," />
        <property name="whitespaceAllowed" value="true" />
    </parser>
        <record name="petowner" class="com.PetOwner ">
            <field name="name" type="String" />     
            <field name="age" type="int" />
            <field name="numberOfPets" type="int"/>
        </record>
    </stream>
</beanio>

And here is my file contents that i am trying to read.. Notice spaces before integer values. There are some design restrictions that i cannot change the data type for age and numberOfPets to String.

 John,   22,  1
 Ashley,   20,  3
ATHER
  • 3,254
  • 5
  • 40
  • 63
  • 1
    You will probably have to write a [Custom Type Handler](http://beanio.org/2.0/docs/reference/index.html#CustomTypeHandlers) that parses the value. This should not be difficult. – Jim Garrison Sep 12 '14 at 21:41
  • Thanks i tried that but i am stuck onto this error. Field property type 'int' is not compatible with assigned type handler named 'intValueHandler' – ATHER Sep 12 '14 at 23:46

1 Answers1

0

You can use trim="true" in the field declaration of your mapping. Then the values are validated after being trimmed.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <beanio xmlns="http://www.beanio.org/2012/03">
<stream name="petowner" format="csv">
    <parser>
        <property name="delimiter" value="," />
        <property name="whitespaceAllowed" value="true" />
    </parser>
    <record name="petowner" class="com.PetOwner ">
        <field name="name" type="String" />     
        <field name="age" type="int" trim="true" />
        <field name="numberOfPets" type="int" trim="true" />
    </record>
</stream>

Michael Brohl
  • 782
  • 5
  • 21