Search code examples
javaudf

not able to split the number value from the string


I'm creating a Udf function for an area conversion program in java. I have the following data:

230Sq.feet
110Sq.yards
8Acres
123Sq.Ft

I want to split the above data like this:

230 Sq.feet
990 Sq.feet
344 Sq.feet
123 Sq.feet

I tried the following code:

public class Areaconversion2 extends EvalFunc<String> {

public String determine_Area (String input) throws IOException
{
    String[] AreaArr = input.split("");
    Double Area;

    if(AreaArr[1].equalsIgnoreCase("Sq.Yards") || AreaArr[1].equalsIgnoreCase("Sq.Yds")) 
    {
    Area = Double.parseDouble(AreaArr[0]);
        Area = Area * 9;
        String Ar = Area.toString() + " Sq.Feet";
        return Ar;
    }
else if(AreaArr[1].equalsIgnoreCase("Acre") || AreaArr[1].equalsIgnoreCase("Acres")) 
{      
        Area = Double.parseDouble(AreaArr[0]);
        Area = Area * 43560;
        String Ar = Area.toString() + " Sq.Feet";
    return Ar;
 }
else if(AreaArr[1].equalsIgnoreCase("Sq.Feet)")||AreaArr[1].equalsIgnoreCase("Sq.Ft"));
      {
          Area = Double.parseDouble(AreaArr[0]); 
       String Ar = Area.toString() + " Sq.Feet";
          return Ar;
      }

    }

public String exec(Tuple input) throws IOException {
    // TODO Auto-generated method stub
     if (input == null || input.size() == 0)
         return null;

     try

     {

         String str = (String)input.get(0);

         return determine_Area(str);
         }catch(Exception e){
              throw new IOException("Caught exception processing input row ", e);
         }
}

}

I got the exception only while processing. Any help will be appreciated.


Solution

  • You can use look-ahead/look-behind matching:

    String[] fields = str.split("(?<=\\d)(?=[A-Z])");
    

    The (?<=\\d) is the zero-length matcher meaning that "a digit must preceed", and (?=[A-Z]) is the zero-length matcher meaning that "a capital letter must be after matched string".

    Tested on your data:

    public static void main(String[] args) {
        String[] inputs = {"230Sq.feet", "110Sq.yards", "8Acres", "123Sq.Ft"};
        for(String input : inputs) {
            String[] fields = input.split("(?<=\\d)(?=[A-Z])");
            System.out.println(fields[0]+" "+fields[1]);
        }
    }
    

    The output is:

    230 Sq.feet
    110 Sq.yards
    8 Acres
    123 Sq.Ft