code: private static int extractCharge(String formula) {
  
          if (!(formula.contains("[") && formula.contains("]") && (formula.contains("+") || formula.contains(HYPHEN_STR) || formula.contains(MINUS_STR))))
              return 0;
  
          boolean finishBreak = false;
          String multiple = "";
          for (int f = 0; f < formula.length(); f++) {
              char thisChar = formula.charAt(f);
              if (thisChar == ']') {
                  // finish
                  finishBreak = true;
              } else if (thisChar == HYPHEN || thisChar == MINUS) {
                  multiple = HYPHEN + multiple;
                  break;
              } else if (thisChar == '+') {
                  break;
              } else if (finishBreak) {
                  multiple += thisChar;
              }
          }
          if (multiple.isEmpty() || multiple.equals(HYPHEN_STR) || multiple.equals(MINUS_STR)) multiple += 1;
          return Integer.valueOf(multiple);
      }
docs: Extract the charge given a molecular formula format [O3S]2-.
  
  @param formula The formula to inspect
  @return        The charge