title: Java String Index out of range error, don't know why
body: <p>I'm writing a program for a school project that reads a chemical formula and determines mass and percent composition. I am trying to write the method to read the formula and create an array containing symbols based upon how often that element shows up in a formula. However, whenever I test the formula reader method, I get an string index out of range error. Can someone tell me what I'm doing wrong?    </p>
  
  <pre><code>//http://www.ptable.com/ was the source for information on elements
  //knowledgedoor.com/ was the source for charges of elements not listed on common ion sheet and not having a charge based on what group it was. I used the one that said more common. Did not use if said with disagreement
  // If an element had multiple charges from common ion sheet, I created multiple objects with similar attributes except charge. Object was named using atomic symbol and a number following corresponding to roman numeral
  
  
  
  
  import java.util.Scanner;
  public class compositionMassMolesAtoms {
  public static double avoNum = (6.02 * Math.pow(10,23));
  static Element H = new Element(1,1,1, "Hydrogen", "H", 1.007, 1, "nonmetal");
  static Element He = new Element(1,18,2, "Helium", "He", 4.002, 0, "nonmetal");
  static Element Li = new Element(2,1,3,"Lithium", "Li", 6.941, 1, "metal");
  //period 2
   static Element Be = new Element(2,2,4,"Beryllium", "Be", 9.012, 2, "metal");
  static Element B = new Element(2,13,5, "Boron", "B", 10.811, 3, "metalloid");
  static Element C = new Element(2,14,6, "Carbon", "C", 12.0107, 4, "metal");
  static Element N = new Element(2,15,7, "Nitrogen", "N", 14.007, -3, "nonmetal");
  static Element O = new Element(2,16,8, "Oxygen", "O", 15.9994, -2, "nonmetal");
  static Element F = new Element(2,17,9, "Fluorine", "F", 18.998,  -1, "nonmetal");
  static Element Ne = new Element(2,18,10, "Neon", "Ne", 20.1797, 0, "nonmetal");
     // period 3
  static Element Na = new Element(3,1,11, "Sodium", "Na", 22.989, 1, "metal");
  static Element Mg = new Element(3,2,12, "Magnesium", "Mg", 24.305, 2, "metal");
  static Element Al = new Element(3,13,13, "Aluminum", "Al", 26.981, 3, "metals");
  static Element Si = new Element(3,14,14, "Silicon", "Si", 28.085, 4, "metalloid");
  static Element P = new Element(3,15,15, "Phosphorous", "P",  30.973, -3, "nonmetal");
  static Element S = new Element(3,16,16, "Sulfur", "S", 32.06, -2, "nonmetal");
  static Element Cl = new Element(3,17,17, "Chlorine", "Cl", 35.45, -1, "nonmetal");
  static Element Ar = new Element(3,18,18, "Argon", "Ar", 39.948, 0, "nonmetal");
     //period 4
  static Element K = new Element(4,1,19, "Potassium", "K", 39.0983, 1, "metal");    
  static Element Ca = new Element(4,2,20, "Calcium", "Ca", 40.078, 2, "metal");
  static Element Sc = new Element(4,3,21, "Scandium", "Sc", 44.955, 3, "metal");
  static Element Ti = new Element(4,4,22, "Titanium", "Ti", 47.867, 4, "metal");
  static Element V = new Element(4,5,23, "Vanadium", "V", 50.9415, 5, "metal");
  static Element Cr2 = new Element(4,6,24, "Chromium", "Cr", 51.9961, 2, "metal");
  static Element Cr3 = new Element(4,6,24, "Chromium", "Cr", 51.9961, 3, "metal");
  static Element Mn2 = new Element(4,7,25, "Manganese", "Mn", 54.938, 2, "metal");
  static Element Mn3 = new Element(4,7,25, "Manganese", "Mn", 54.938, 3, "metal");
  static Element Mn4 = new Element(4,7,25, "Manganese", "Mn", 54.938, 4, "metal");
  static Element Fe2 = new Element(4,8,26, "Iron", "Fe", 55.845, 2, "metal");
  static Element Fe3 = new Element(4,8,26, "Iron", "Fe", 55.845, 3, "metal");
  static Element Co2 = new Element(4,9,27, "Cobalt", "Co", 58.933, 2, "metal");
  static Element Co3 = new Element(4,9,27, "Cobalt", "Co", 58.933, 3, "metal");
  static Element Ni2 = new Element(4,10,28, "Nickel", "Ni", 58.6934, 2, "metal");
  static Element Ni3 = new Element(4,10,28, "Nickel", "Ni", 58.6934, 3, "metal");
  static Element Cu1 = new Element(4,11,29, "Copper", "Cu", 63.546, 1, "metal");
  static Element Cu2 = new Element(4,11,29, "Copper", "Cu", 63.546, 2, "metal");
  static Element Zn = new Element(4,12,30, "Zinc", "Zn", 65.38, 2, "metal");
  static Element Ga = new Element(4,13,31, "Gallium", "Ga", 69.723, 3, "metal");
  static Element Ge = new Element(4,14,32, "Germanium", "Ge", 72.63, 4, "metalloid");
  static Element As = new Element(4,15,33, "Arsenic", "As", 74.9216, -3, "metalloid");
  static Element Se = new Element(4,16,34, "Selenium", "Se", 78.96, -2, "nonmetal");
  static Element Br = new Element(4,17,35, "Bromine", "Br", 79.904, -1, "nonmetal");
  static Element Kr = new Element(4,18,36, "Krypton", "Kr", 83.798, 0, "nonmetal");
     //period 5
  static Element Rb = new Element(5,1,37, "Rubidium", "Rb", 85.4678, 1, "metal");  
  static Element Sr = new Element(5,2,38, "Strontium", "Sr", 87.62, 2, "metal");
  static Element Y = new Element(5,3,39, "Yttrium", "Y", 88.90585, 3, "metal");
  static Element Zr = new Element(5,4,40, "Zirconium", "Zr", 91.224, 4, "metal");
  static Element Nb = new Element(5,5,41, "Niobium", "Nb", 92.90638, 5, "metal");
  static Element Mo = new Element(5,6,42, "Molybdenum", "Mo", 95.96, 6, "metal");
  static Element Tc = new Element(5,7,43, "Technetium", "Tc", 98, 4, "metal");
  static Element Ru = new Element(5,8,44, "Ruthenium", "Ru", 101.07, 3, "metal");
  static Element Rh = new Element(5,9,45, "Rhodium", "Rh", 102.90550, 3, "metal");
  static Element Pd = new Element(5,10,46, "Palladium", "Pd", 106.43, 2, "metal");
  static Element Ag = new Element(5,11,47, "Silver", "Ag", 107.8682, 1, "metal");
  
     // charges after this point are not accurate, as they are just set to one.
     // need to go back and correct charges
  
  static Element Cd = new Element(5,12,48, "Cadmium", "Cd", 112.411, 1, "metal");
  static Element In = new Element(5,13,49, "Indium", "In", 114.818, 1, "metal");
  static Element Sn = new Element(5,14,50, "Tin", "Sn", 118.710, 1, "metal");
  static Element Sb = new Element(5,15,51, "Antimony", "Sb", 121.760, 1,"metalloid");
  static Element Te = new Element(5,16,52, "Tellurium", "Te", 127.60, 1, "metalloid");
  static Element I = new Element(5,17,53, "Iodine", "I", 126.90, 1, "nonmetal");
  static Element Xe = new Element(5,18,54, "Xenon", "Xe", 131.293, 1, "nonmetal");
     // period 6
  static Element Cs = new Element(6,1,55, "Caesium", "Cs", 132.90, 1, "metal");
  static Element Ba = new Element(6,2,56, "Barium", "Ba", 137.327, 1, "metal");
     // Lanthanoids
  static Element La = new Element(8,1,57, "Lanthanum", "La", 138.90, 1, "metal");
  static Element Ce = new Element(8,2,58, "Cerium", "Ce", 140.116, 1, "metal");
  static Element Pr = new Element(8,3,59, "Praseodymium", "Pr", 140.90, 1, "metal");
  static Element Nd = new Element(8,4,60, "Neodymium", "Nd", 144.242, 1, "metal");
  static Element Pm = new Element(8,5,61, "Promethium", "Pm", 145, 1, "metal");
  static Element Sm = new Element(8,6,62, "Samarium", "Sm", 150.36, 1, "metal");
  static Element Eu = new Element(8,7,63, "Europium", "Eu", 151.964, 1, "metal");
  static Element Gd = new Element(8,8,64, "Gadolinium", "Gd", 157.25, 1, "metal");
  static Element Tb = new Element(8,9,65, "Terbium", "Tb", 158.92, 1, "metal");
  static Element Dy = new Element(8,10,66, "Dysprosium", "Dy", 162.5, 1, "metal");
  static Element Ho = new Element(8,11,67, "Holmium", "Ho", 164.93, 1, "metal");
  static Element Er = new Element(8,12,68, "Erbium", "Er", 167.259, 1, "metal");
  static Element Tm = new Element(8,13,69, "Thulium", "Tm", 168.93, 1, "metal");
  static Element Yb = new Element(8,14,70, "Ytterbium", "Yb", 173.054, 1, "metal");
  static Element Lu = new Element(8,15,71, "Lutetium", "Lu", 174.9668, 1, "metal");
     // End of lanthanoids, period 6
  static Element Hf = new Element(6,4,72, "Hafnium", "Hf", 178.49, 1, "metal");
  static Element Ta = new Element(6,5,73, "Tantalum", "Ta", 180.94, 1, "metal");
  static Element W = new Element(6,6,74, "Tungsten", "W", 183.84, 1, "metal");
  static Element Re = new Element(6,7,75, "Rhenium", "Re", 186.207, 1, "metal");
  static Element Os = new Element(6,8,76,"Osmium", "Os", 190.23, 1, "metal"); 
  static Element Ir = new Element(6,9,77, "Iridium", "Ir", 192.217, 1, "metal");
  static Element Pt = new Element(6,10,78, "Platinum", "Pt", 195.084, 1, "metal");
  static Element Au = new Element(6,11,79, "Gold", "Au", 196.96,1,"metal");
  static Element Hg = new Element(6,12,80,"Mercury", "Hg", 200.59, 1, "metal");
  static Element Tl = new Element(6,13,81, "Thallium", "Tl", 204.38, 1, "metal");
  static Element Pb = new Element(6,14,82, "Lead", "Pb", 207.2, 1, "metal");
  static Element Bi = new Element(6,15,83, "Bismuth", "Bi", 208.98, 1, "metal");
  
     // most elements after this point have unconfirmed mass do to instable isotopes
     // doesn’t include uranium, does include Technetium, Thorium, Protactinium and Promethium
  
  static Element Po = new Element(6,16,84, "Polonium", "Po", 209, 1, "metalloid");
  static Element At = new Element(6,17,85, "Astatine", "At", 210, 1, "nonmetal");
  static Element Rn = new Element(6,18,86, "Radon", "Rn", 222, 1, "nonmetal");
     // Period 7
  static Element Fr = new Element(7,1,87, "Francium", "Fr",  223, 1, "metal");
  static Element Ra = new Element(7,2,88, "Radium", "Ra", 226, 1, "metal");
     // Beginning of Actinoids
  static Element Ac = new Element(9,1,89, "Actinium", "Ac", 227, 1, "metal");
  static Element Th = new Element(9,2,90, "Thorium", "Th", 232.03,1, "metal");
  static Element Pa = new Element(9,3,91, "Protactinium", "Pa", 231.03, 1, "metal");
  static Element U = new Element(9,4,92, "Uranium", "U", 238.02, 1, "metal");
  static Element Np = new Element(9,5,93, "Neptunium", "Np", 237, 1, "metal");
  static Element Pu = new Element(9,6,94, "Plutonium", "Pu", 244, 1, "metal");
  static Element Am = new Element(9,7,95, "Americium", "Am", 243, 1, "metal");
  static Element Cm = new Element(9,8,96, "Curium", "Cm", 247, 1, "metal");
  static Element Bk = new Element(9,9,97, "Berkelium", "Bk", 247, 1, "metal");
  static Element Cf = new Element(9,10,98, "Californium", "Cf", 251, 1, "metal");
  static Element Es = new Element(9,11,99, "Einsteinium", "Es", 252, 1, "metal");
  static Element Fm = new Element(9,12,100, "Fermium", "Fm", 257, 1, "metal");
  static Element Md = new Element(9,13,101, "Mendelevium", "Md",  258, 1, "metal");
  static Element No = new Element(9,14,102, "Nobelium", "No", 259, 1, "metal");
  static Element Lr = new Element(9,15, 103, "Lawrencium", "Lr", 262,1,"metal");
     // Actinoids finished. Not going to do element with an unknown state as of 1/26/17 
  
  
   public static double getMass(String sym) {
      if (sym.equals("H")) {
         return H.mass;
      }
      else if(sym.equals("He")) {
         return He.mass;
      }
      else if(sym.equals("Li")) {
         return Li.mass;
      }
      else if(sym.equals("Be")) {
         return Be.mass;
      }
      else if(sym.equals("B")) {
         return B.mass;
      }
      else if(sym.equals("C")) {
         return C.mass;
      }
      else if(sym.equals("N")) {
         return N.mass;
      }
      else if(sym.equals("O")) {
         return O.mass;
      }
      else if(sym.equals("F")) {
         return F.mass;   
      }
      else if(sym.equals("Ne")) {
         return Ne.mass;
      }
      else if(sym.equals("Na")) {
         return Na.mass;
      }
      else if(sym.equals("Mg")) {
         return Mg.mass;
      }
      else if(sym.equals("Al")) {
          return Al.mass;
      }
      else if(sym.equals("Si")) {
          return Si.mass;
      }
      else if(sym.equals("P")) {
          return P.mass;
      }
      else if(sym.equals("S")) {
          return Si.mass;
      }
      else if(sym.equals("Cl")) {
          return Cl.mass;
      }
      else if(sym.equals("Ar")) {
          return Ar.mass;
      }
      else if(sym.equals("K")) {
          return K.mass;
      }
      else if(sym.equals("Ca")) {
          return Ca.mass;
      }
      else if(sym.equals("Sc")) {
          return Sc.mass;
      }
      else if(sym.equals("Ti")) {
          return Ti.mass;
      }
      else if(sym.equals("V")) {
          return V.mass;
      }
      else if(sym.equals("Cr")) {
          return Cr2.mass;
      }
      else if(sym.equals("Mn")) {
          return Mn2.mass;
      }
      else if(sym.equals("Fe")) {
          return Fe2.mass;
      }
      else if(sym.equals("Co")) {
          return Co2.mass;
      }
      else if(sym.equals("Ni")) {
          return Ni2.mass;
      }
      else if(sym.equals("Cu")) {
          return Cu2.mass;
      }
      else if(sym.equals("Zn")) {
          return Zn.mass;
      }
      else if(sym.equals("Ga")) {
          return Ga.mass;
      }
      else if(sym.equals("Ge")) {
          return Ge.mass;
      }
      else if(sym.equals("As")) {
         return As.mass;
      }
      else if(sym.equals("Se")) {
          return Se.mass;
      }
      else if(sym.equals("Br")) {
          return Br.mass;
      }
      else if(sym.equals("Kr")) {
          return Kr.mass;
      }
      else if(sym.equals("Rb")) {
          return Rb.mass;
      }
      else if(sym.equals("Sr")) {
          return Sr.mass;
      }
      else if(sym.equals("Y")) {
          return Y.mass;
      }
      else if(sym.equals("Zr")) {
          return Zr.mass;
      }
      else if(sym.equals("Nb")) {
          return Nb.mass;
      }
      else if(sym.equals("Mo")) {
          return Mo.mass;
      }
      else if(sym.equals("Tc")) {
          return Tc.mass;
      }
      else if(sym.equals("Ru")) {
          return Ru.mass;
      }
      else if(sym.equals("Rh")) {
          return Rh.mass;
      }
      else if(sym.equals("Pd")) {
          return Pd.mass;
      }
      else if(sym.equals("Ag")) {
          return Ag.mass;
      }
      else if(sym.equals("Cd")) {
          return Cd.mass;
      }
      else if(sym.equals("In")) {
          return In.mass;
      }
      else if(sym.equals("Sn")) {
          return Sn.mass;
      }
      else if(sym.equals("Sb")) {
          return Sb.mass;
      }
      else if(sym.equals("Te")) {
          return Te.mass;
      }
      else if(sym.equals("I")) {
          return I.mass;
      } 
      else if(sym.equals("Xe")) {
          return Xe.mass;
      }
      else if(sym.equals("Cs")) {
          return Cs.mass;
      }
      else if(sym.equals("Ba")) {
          return Ba.mass;
      }
      else if(sym.equals("La")) {
          return La.mass;
      } 
      else if(sym.equals("Ce")) {
          return Ce.mass;
      }
      else if(sym.equals("Pr")) {
          return Pr.mass;
      }
      else if(sym.equals("Nd")) {
          return Nd.mass;
      }
      else if(sym.equals("Pm")) {
          return Pm.mass;
      }
      else if(sym.equals("Sm")) {
          return Sm.mass;
      }
      else if(sym.equals("Eu")) {
          return Eu.mass;
      }
      else if(sym.equals("Gd")) {
          return Gd.mass;
      }
      else if(sym.equals("Tb")) {
          return Tb.mass;
      }
      else if(sym.equals("Dy")) {
          return Dy.mass;
      }
      else if(sym.equals("Ho")) {
          return Ho.mass;
      }
      else if(sym.equals("Er")) {
          return Er.mass;
      }
      else if(sym.equals("Tm")) {
          return Tm.mass;
      }       
      else if(sym.equals("Yb")) {
          return Yb.mass;
      }
      else if(sym.equals("Lu")) {
          return Si.mass;
      } 
      else if(sym.equals("Hf")) {
          return Hf.mass;
      }
      else if(sym.equals("Ta")) {
          return Ta.mass;
      }
      else if(sym.equals("W")) {
          return W.mass;
      }
      else if(sym.equals("Re")) {
          return Re.mass;
      }
      else if(sym.equals("Os")) {
          return Os.mass;
      }
      else if(sym.equals("Ir")) {
          return Ir.mass;
      }
      else if(sym.equals("Pt")) {
          return Pt.mass;
      }
      else if(sym.equals("Au")) {
          return Au.mass;
      }
      else if(sym.equals("Hg")) {
          return Hg.mass;
      }
      else if(sym.equals("Tl")) {
          return Tl.mass;
      }
      else if(sym.equals("Pb")) {
          return Pb.mass;
      }
      else if(sym.equals("Bi")) {
          return Bi.mass;
      }
      else if(sym.equals("Po")) {
          return Po.mass;
      }
      else if(sym.equals("At")) {
          return At.mass;
      } 
      else if(sym.equals("Rn")) {
          return Rn.mass;
      }
      else if(sym.equals("Fr")) {
          return Fr.mass;
      }
      else if(sym.equals("Ra")) {
          return Ra.mass;
      }
      else if(sym.equals("Ac")) {
          return Ac.mass;
      }
      else if(sym.equals("Th")) {
          return Th.mass;
      }
      else if(sym.equals("Pa")) {
          return Pa.mass;
      } 
      else if(sym.equals("U")) {
          return U.mass;
      }
      else if(sym.equals("Np")) {
          return Np.mass;
      }
      else if(sym.equals("Pu")) {
          return Pu.mass;
      }
      else if(sym.equals("Am")) {
          return Am.mass;
      }
      else if(sym.equals("Cm")) {
          return Cm.mass;
      }
      else if(sym.equals("Bk")) {
          return Bk.mass;
      }
      else if(sym.equals("Cf")) {
          return Cf.mass;
      }
      else if(sym.equals("Es")) {
          return Es.mass;
      }
      else if(sym.equals("Fm")) {
          return Fm.mass;
      }
      else if(sym.equals("Md")) {
          return Md.mass;
      }
      else if(sym.equals("No")) {
          return No.mass;
      }
      else if(sym.equals("Lr")) {
          return Lr.mass;
      }
  
      return 50000;
  }
  public static String[] readChemForm(String chemForm) {
      int lC = 0;
      int arrCou = 0;
  
      String[] elements = new String[10000];
  
      while(lC &lt; chemForm.length()) {
          int elemCoun = 0;
          int jut = chemForm.length()-lC;
          if (jut &gt; 3) {
               jut = 3;                
          }
          else{}
          Boolean lengthCheck3 = true;
          Boolean lengthCheck2 = true;
          Boolean lengthCheck1 = true;
          if (jut == 2) { 
              lengthCheck3 = false; }
          else if(jut == 1) {
              lengthCheck2 = false;
              lengthCheck3 = true;
          }
          else if(jut &lt;= 0) {
              break; 
          }
          else {}
          String massCompF = chemForm.substring(lC,(lC+jut));  
          System.out.println(jut);
           if ((massCompF.substring((lC+1),(lC+2)).toLowerCase().equals(massCompF.substring((lC+1),(lC+2)))) &amp;&amp; (massCompF.length() &gt; 2)) {
               if ((massCompF.length() &gt; 3) &amp;&amp; (massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("2") || massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("3") || massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("4") || massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("5") || massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("6") || massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("7") || massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("8") || massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("9") ||massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("10") || massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("11") || massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("12") ||massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("13") || massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("14") || massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("15") || massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("16") || massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("17") ||  massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("18") || massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("19") || massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("20") || massCompF.substring((lC+2),(lC+3)).toLowerCase().equals("21"))) {
                   String placeHolder = massCompF.substring((lC+2),(lC+3));
                   if (placeHolder.equals("2")) {
                      elemCoun = 2;
                   }
                   else if (placeHolder.equals("3")) {
                      elemCoun = 3; 
                   }
                   else if (placeHolder.equals("4")) {
                      elemCoun = 4;
                   }
                   else if (placeHolder.equals("5")) {
                      elemCoun = 5;
                   }
                   else if (placeHolder.equals("6")) {
                      elemCoun = 6;
                   }
                   else if (placeHolder.equals("7")) {
                      elemCoun = 7;
                   }
                   else if (placeHolder.equals("8")) {
                       elemCoun = 8;
                   }
                   else if (placeHolder.equals("10")) {
                       elemCoun = 10;
                   }
                   else if (placeHolder.equals("11")) {
                       elemCoun = 11;
                   }
                   else if (placeHolder.equals("12")) {
                       elemCoun = 12;
                   }
                   else if (placeHolder.equals("13")) {
                       elemCoun = 13;
                   }
                   else if (placeHolder.equals("14")) {
                       elemCoun = 14;
                   }
                   else if (placeHolder.equals("15")) {
                       elemCoun = 15;
                   }
                   else if (placeHolder.equals("15")) {
                      elemCoun = 15;
                   }
                   else if (placeHolder.equals("16")) {
                       elemCoun = 16;
                   }
                   else if (placeHolder.equals("17")) {
                       elemCoun = 17;
                   }
                   else if (placeHolder.equals("18")) {
                       elemCoun = 18;
                   }
                   else if (placeHolder.equals("19")) {
                       elemCoun = 19;
                   } 
                   else if (placeHolder.equals("20")) {
                       elemCoun = 20;
                   }
                   else if (placeHolder.equals("21")) {
                       elemCoun = 21;
                   }
                   else{}
                   String sym = massCompF.substring((lC),(lC+2));
                   for (int i = 0; i &lt;= elemCoun; i++) {
                      elements[arrCou] = sym;
                   }
                   lC += 3;
                }
                else{
                  String sym = massCompF.substring((lC),(lC+2));
                  elements[arrCou] = sym;
                  lC += 2;
               }
           }
           else if((!(massCompF.substring((lC+1),(lC+2)).toLowerCase().equals(massCompF.substring((lC+1),(lC+2))))) &amp;&amp; (massCompF.length() &gt; 1)) {
              if ((massCompF.length() &gt; 2) &amp;&amp; (massCompF.substring((lC+1),(lC+2)).toLowerCase().equals("2") || massCompF.substring((lC+1),(lC+2)).toLowerCase().equals("3") || massCompF.substring((lC+1),(lC+2)).toLowerCase().equals("4") || massCompF.substring((lC+1),(lC+2)).toLowerCase().equals("5") || massCompF.substring((lC+1),(lC+2)).toLowerCase().equals("6") || massCompF.substring((lC+1),(lC+2)).toLowerCase().equals("7") || massCompF.substring((lC+1),(lC+2)).toLowerCase().equals("8") || massCompF.substring((lC+1),(lC+2)).toLowerCase().equals("9"))){
                  String placeHolder = massCompF.substring((lC+1),(lC+2));
                  if (placeHolder.equals("2")) {
                     elemCoun = 2;
                  }
                  else if (placeHolder.equals("3")) {
                     elemCoun = 3; 
                  }
                  else if (placeHolder.equals("4")) {
                      elemCoun = 4;
                  }
                  else if (placeHolder.equals("5")) {
                      elemCoun = 5;
                  }
                  else if (placeHolder.equals("6")) {
                      elemCoun = 6;
                  }
                  else if (placeHolder.equals("7")) {
                      elemCoun = 7;
                  }
                  else if (placeHolder.equals("8")) {
                      elemCoun = 8;
                  }
                  else if (placeHolder.equals("10")) {
                      elemCoun = 10;
                  }
                  else if (placeHolder.equals("11")) {
                      elemCoun = 11;
                  }
                  else if (placeHolder.equals("12")) {
                      elemCoun = 12;
                  }
                  else if (placeHolder.equals("13")) {
                      elemCoun = 13;
                  }
                  else if (placeHolder.equals("14")) {
                      elemCoun = 14;
                  }
                  else if (placeHolder.equals("15")) {
                      elemCoun = 15;
                  }
                  else if (placeHolder.equals("15")) {
                      elemCoun = 15;
                  }
                  else if (placeHolder.equals("16")) {
                      elemCoun = 16;
                  }
                  else if (placeHolder.equals("17")) {
                      elemCoun = 17;
                  }
                  else if (placeHolder.equals("18")) {
                      elemCoun = 18;
                  }
                  else if (placeHolder.equals("19")) {
                      elemCoun = 19;
                  } 
                  else if (placeHolder.equals("20")) {
                      elemCoun = 20;
                  }
                  else if (placeHolder.equals("21")) {
                      elemCoun = 21;
                  }
                  else{}
                  String sym = massCompF.substring((lC),(lC+1));
                  for (int i = 0; i &lt;= elemCoun; i++) {
                      elements[arrCou] = sym;
                  }
                  lC += 2;
              }
              else{}
              String sym = massCompF.substring((lC),(lC+1));
              for (int i = 0; i &lt;= elemCoun; i++) {
                   elements[arrCou] = sym;
              }
              lC++;
          }
       } 
      return elements;
  }
  public static void main(String[] args){
      boolean quit = false;
      String error = "You input something that doesn't exist. Or, I forgot an element. /n Email me at rohanrtkurup@gmail.com if you find the element you input on the periodic table and you got this error.";
  
      while(quit == false) {
          Scanner n = new Scanner(System.in);
  
          System.out.println("Hello");
          System.out.println("What program do you want to run?");
          System.out.println("0: end program, 1: Gram to Gram 2:moles to moles, 3:Find mass and element composition"); 
          int pr1 = n.nextInt();
  
           switch(pr1) {
             case 3:            
             System.out.println("What is the Chemical Formula");
             String chemForm = n.next();
             String[] elements = readChemForm(chemForm);
  
             double totalMass = 0.0;
             int lt = 0;
             while (lt &lt; elements.length) {
                  totalMass += getMass(elements[lt]);
                  lt++;
             }
             System.out.println("The total mass of that compound is: " + totalMass);
             break;
             default:
             System.out.println("");
          }
          for(int f = 0; f &lt;= 5; f++) {  
             System.out.println("");
          }
      }   
  }
  </code></pre>
  
  <p>}</p>