All pastes #804375 Raw Edit

Untitled

public text v1 · immutable
·published 2007-12-03 12:42 UTC
rendered paste body
public static final int SHORTEST_POSSIBLE_PHONE_NUM=6; // why not 3? trade off since too many occurances of 3 and 4 nums that arent real numbers.
     private Vector extractPhoneNumbersFromString(String body)
     {
         char[] bodyC = body.toCharArray();
         int startIndex = -1;    
         int endIndex   = -1;
         char lastChar='x';
         Vector v = new Vector();
                   
         for (int i=0; i<bodyC.length; i++)
         {
             if (startIndex==-1)// if it starts with an acceptable char which could be start of a number 
             {
                 if ( bodyC[i]=='0' || bodyC[i]=='1' || bodyC[i]=='2' || bodyC[i]=='3' || bodyC[i]=='4' || bodyC[i]=='5' ||
                      bodyC[i]=='6' || bodyC[i]=='7' || bodyC[i]=='8' || bodyC[i]=='9' || bodyC[i]=='(' || bodyC[i]=='+'   )
                 {
                      startIndex=i;
                 }
             }
             // check if this char is an acceptable one that could be in a phone number and that there was not 2 spaces in a row 
             boolean acceptable = ( acceptable(bodyC[i],1) && !(lastChar==' ' && bodyC[i]==' ') );
             if ( acceptable ) // middle of a number and acceptable
             {
                 lastChar=bodyC[i];
                 endIndex=i;
             }
             else
             {
                 if ( (startIndex>0 && endIndex>0) && (endIndex-startIndex>=SHORTEST_POSSIBLE_PHONE_NUM) )
                 {
                     v.addElement( body.substring(startIndex,endIndex) ); //add the number to the vector
                 }
                 startIndex=-1;
                 endIndex=-1;
             }
         }
         return v;
     }