0

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.

Raein Hashemi
  • 3,346
  • 4
  • 22
  • 33

2 Answers2

0

Instead of using split() you could go for Pattern and Matcher.

public static void main(String[] args) {
    String s = "230Sq.feet";
    Pattern p = Pattern.compile("(\\d+)(\\D+)"); // group 1 is the number part and group 2 is everything that follows the number part
    Matcher m = p.matcher(s);
    while (m.find()) {
        System.out.println(m.group(1));
        System.out.println(m.group(2));
    }
}
O/P :
230
Sq.feet
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

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
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334