private MGRSComponents breakMGRSString(String MGRSString){
  int num_digits;
  int num_letters;
  int i=0;
  int j=0;
  long error_code=MGRS_NO_ERROR;
  int zone=0;
  int[] letters=new int[3];
  long easting=0;
  long northing=0;
  int precision=0;
  while (i < MGRSString.length() && MGRSString.charAt(i) == ' ') {
    i++;
  }
  j=i;
  while (i < MGRSString.length() && Character.isDigit(MGRSString.charAt(i))) {
    i++;
  }
  num_digits=i - j;
  if (num_digits <= 2)   if (num_digits > 0) {
    zone=Integer.parseInt(MGRSString.substring(j,i));
    if ((zone < 1) || (zone > 60))     error_code|=MGRS_STRING_ERROR;
  }
 else   error_code|=MGRS_STRING_ERROR;
  j=i;
  while (i < MGRSString.length() && Character.isLetter(MGRSString.charAt(i))) {
    i++;
  }
  num_letters=i - j;
  if (num_letters == 3) {
    letters[0]=alphabet.indexOf(Character.toUpperCase(MGRSString.charAt(j)));
    if ((letters[0] == LETTER_I) || (letters[0] == LETTER_O))     error_code|=MGRS_STRING_ERROR;
    letters[1]=alphabet.indexOf(Character.toUpperCase(MGRSString.charAt(j + 1)));
    if ((letters[1] == LETTER_I) || (letters[1] == LETTER_O))     error_code|=MGRS_STRING_ERROR;
    letters[2]=alphabet.indexOf(Character.toUpperCase(MGRSString.charAt(j + 2)));
    if ((letters[2] == LETTER_I) || (letters[2] == LETTER_O))     error_code|=MGRS_STRING_ERROR;
  }
 else   error_code|=MGRS_STRING_ERROR;
  j=i;
  while (i < MGRSString.length() && Character.isDigit(MGRSString.charAt(i))) {
    i++;
  }
  num_digits=i - j;
  if ((num_digits <= 10) && (num_digits % 2 == 0)) {
    int n;
    double multiplier;
    n=num_digits / 2;
    precision=n;
    if (n > 0) {
      easting=Integer.parseInt(MGRSString.substring(j,j + n));
      northing=Integer.parseInt(MGRSString.substring(j + n,j + n + n));
      multiplier=Math.pow(10.0,5 - n);
      easting*=multiplier;
      northing*=multiplier;
    }
 else {
      easting=0;
      northing=0;
    }
  }
 else   error_code|=MGRS_STRING_ERROR;
  last_error=error_code;
  if (error_code == MGRS_NO_ERROR)   return new MGRSComponents(zone,letters[0],letters[1],letters[2],easting,northing,precision);
  return null;
}

private MGRSComponents breakMGRSString ( String MGRSString ) { int num_digits ; int num_letters ; int i = 0 ; int j = 0 ; long error_code = MGRS_NO_ERROR ; int zone = 0 ; int [ ] letters = new int [ 3 ] ; long easting = 0 ; long northing = 0 ; int precision = 0 ; while ( i < MGRSString . length ( ) && MGRSString . charAt ( i ) == ' ' ) { i ++ ; } j = i ; while ( i < MGRSString . length ( ) && Character . isDigit ( MGRSString . charAt ( i ) ) ) { i ++ ; } num_digits = i - j ; if ( num_digits <= 2 ) if ( num_digits > 0 ) { zone = Integer . parseInt ( MGRSString . substring ( j , i ) ) ; if ( ( zone < 1 ) || ( zone > 60 ) ) error_code |= MGRS_STRING_ERROR ; } else error_code |= MGRS_STRING_ERROR ; j = i ; while ( i < MGRSString . length ( ) && Character . isLetter ( MGRSString . charAt ( i ) ) ) { i ++ ; } num_letters = i - j ; if ( num_letters == 3 ) { letters [ 0 ] = alphabet . indexOf ( Character . toUpperCase ( MGRSString . charAt ( j ) ) ) ; if ( ( letters [ 0 ] == LETTER_I ) || ( letters [ 0 ] == LETTER_O ) ) error_code |= MGRS_STRING_ERROR ; letters [ 1 ] = alphabet . indexOf ( Character . toUpperCase ( MGRSString . charAt ( j + 1 ) ) ) ; if ( ( letters [ 1 ] == LETTER_I ) || ( letters [ 1 ] == LETTER_O ) ) error_code |= MGRS_STRING_ERROR ; letters [ 2 ] = alphabet . indexOf ( Character . toUpperCase ( MGRSString . charAt ( j + 2 ) ) ) ; if ( ( letters [ 2 ] == LETTER_I ) || ( letters [ 2 ] == LETTER_O ) ) error_code |= MGRS_STRING_ERROR ; } else error_code |= MGRS_STRING_ERROR ; j = i ; while ( i < MGRSString . length ( ) && Character . isDigit ( MGRSString . charAt ( i ) ) ) { i ++ ; } num_digits = i - j ; if ( ( num_digits <= 10 ) && ( num_digits % 2 == 0 ) ) { int n ; double multiplier ; n = num_digits / 2 ; precision = n ; if ( n > 0 ) { easting = Integer . parseInt ( MGRSString . substring ( j , j + n ) ) ; northing = Integer . parseInt ( MGRSString . substring ( j + n , j + n + n ) ) ; multiplier = Math . pow ( 10.0 , 5 - n ) ; easting *= multiplier ; northing *= multiplier ; } else { easting = 0 ; northing = 0 ; } } else error_code |= MGRS_STRING_ERROR ; last_error = error_code ; if ( error_code == MGRS_NO_ERROR ) return new MGRSComponents ( zone , letters [ 0 ] , letters [ 1 ] , letters [ 2 ] , easting , northing , precision ) ; return null ; }
the function break_mgrs_string breaks down an mgrs coordinate string into its component parts . updates last_error .
