Finding Molecular Weight in Java

This article is basically not for everyone, it is for those who deal with all chemistry related subjects or students who are have a need to find an element's molecular weight. Molecular weight basically is the element's weight and the finding the expression full molecular weight.
 
For Eg: H2O; in this case we can say that there are two Hydrogen atoms and 1 Oxygen atom so for the molecular weight calculation the expression would be say Hydrogen's atomic weight times 2 + Oxygen's atomic weight. Say for example the element Hydrogen has the atomic weight of 1 and oxygen has the atomic weight of 16. Then the expression result would be 1X2+16=18.
 
The following is the code for finding the molecular weight in Java: 
  1. import java.util.Enumeration;  
  2. import java.util.Hashtable;  
  3. import java.io.*;  
  4.   
  5. public class ElementCalculation {  
  6.     static Hashtable hm;  
  7.     Hashtable[][] reactpro;  
  8.     Hashtable[] templist;  
  9.     Hashtable brentry = new Hashtable();  
  10.     double output, finaloutput;  
  11.     int newid = 0, roundopen = 0, boxopen = 0, fullLen = 0, indexLen = 0, hmrpos = 0, hmcpos = 0, digitvalue = 0, tempindex = 0, suffix = 0;  
  12.     String temp = "", args = "";  
  13.     boolean keyexists = false, yesno = false;  
  14.     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
  15.     public ElementCalculation() {  
  16.         setInitialValues();  
  17.         initializeDefaultreactPro();  
  18.     }  
  19.     public void dispose() {  
  20.         temp = "";  
  21.         roundopen = boxopen = tempindex = hmrpos = hmcpos = digitvalue = fullLen = indexLen = newid = suffix = 0;  
  22.         brentry.clear();  
  23.         keyexists = yesno = false;  
  24.         finaloutput = output = 0;  
  25.     }  
  26.     private static void setInitialValues() {  
  27.         hm = new Hashtable();  
  28.         hm.put("H"new Double(1.0079));  
  29.         hm.put("He"new Double(4.0026));  
  30.         hm.put("Li"new Double(6.941));  
  31.         hm.put("Be"new Double(9.0122));  
  32.         hm.put("B"new Double(10.811));  
  33.         hm.put("C"new Double(12.0107));  
  34.         hm.put("N"new Double(14.0067));  
  35.         hm.put("O"new Double(15.9994));  
  36.         hm.put("F"new Double(18.9984));  
  37.         hm.put("Ne"new Double(20.1797));  
  38.         hm.put("Na"new Double(22.9897));  
  39.         hm.put("Mg"new Double(24.305));  
  40.         hm.put("Al"new Double(26.9815));  
  41.         hm.put("Si"new Double(28.0855));  
  42.         hm.put("P"new Double(30.9738));  
  43.         hm.put("S"new Double(32.065));  
  44.         hm.put("Cl"new Double(35.453));  
  45.         hm.put("K"new Double(39.0983));  
  46.         hm.put("Ar"new Double(39.948));  
  47.         hm.put("Ca"new Double(40.078));  
  48.         hm.put("Sc"new Double(44.9559));  
  49.         hm.put("Ti"new Double(47.867));  
  50.         hm.put("V"new Double(50.9415));  
  51.         hm.put("Cr"new Double(51.9961));  
  52.         hm.put("Mn"new Double(54.938));  
  53.         hm.put("Fe"new Double(55.845));  
  54.         hm.put("Ni"new Double(58.6934));  
  55.         hm.put("Co"new Double(58.9332));  
  56.         hm.put("Cu"new Double(63.546));  
  57.         hm.put("Zn"new Double(65.39));  
  58.         hm.put("Ga"new Double(69.723));  
  59.         hm.put("Ge"new Double(72.64));  
  60.         hm.put("As"new Double(74.9216));  
  61.         hm.put("Se"new Double(78.96));  
  62.         hm.put("Br"new Double(79.904));  
  63.         hm.put("Kr"new Double(83.8));  
  64.         hm.put("Rb"new Double(85.4678));  
  65.         hm.put("Sr"new Double(87.62));  
  66.         hm.put("Y"new Double(88.9059));  
  67.         hm.put("Zr"new Double(91.224));  
  68.         hm.put("Nb"new Double(92.9064));  
  69.         hm.put("Mo"new Double(95.94));  
  70.         hm.put("Tc"new Double(98));  
  71.         hm.put("Ru"new Double(101.07));  
  72.         hm.put("Rh"new Double(102.9055));  
  73.         hm.put("Pd"new Double(106.42));  
  74.         hm.put("Ag"new Double(107.8682));  
  75.         hm.put("Cd"new Double(112.411));  
  76.         hm.put("In"new Double(114.818));  
  77.         hm.put("Sn"new Double(118.71));  
  78.         hm.put("Sb"new Double(121.76));  
  79.         hm.put("I"new Double(126.9045));  
  80.         hm.put("Te"new Double(127.6));  
  81.         hm.put("Xe"new Double(131.293));  
  82.         hm.put("Cs"new Double(132.9055));  
  83.         hm.put("Ba"new Double(137.327));  
  84.         hm.put("La"new Double(138.9055));  
  85.         hm.put("Ce"new Double(140.116));  
  86.         hm.put("Pr"new Double(140.9077));  
  87.         hm.put("Nd"new Double(144.24));  
  88.         hm.put("Pm"new Double(145));  
  89.         hm.put("Sm"new Double(150.36));  
  90.         hm.put("Eu"new Double(151.964));  
  91.         hm.put("Gd"new Double(157.25));  
  92.         hm.put("Tb"new Double(158.9253));  
  93.         hm.put("Dy"new Double(162.5));  
  94.         hm.put("Ho"new Double(164.9303));  
  95.         hm.put("Er"new Double(167.259));  
  96.         hm.put("Tm"new Double(168.9342));  
  97.         hm.put("Yb"new Double(173.04));  
  98.         hm.put("Lu"new Double(174.967));  
  99.         hm.put("Hf"new Double(178.49));  
  100.         hm.put("Ta"new Double(180.9479));  
  101.         hm.put("W"new Double(183.84));  
  102.         hm.put("Re"new Double(186.207));  
  103.         hm.put("Os"new Double(190.23));  
  104.         hm.put("Ir"new Double(192.217));  
  105.         hm.put("Pt"new Double(195.078));  
  106.         hm.put("Au"new Double(196.9665));  
  107.         hm.put("Hg"new Double(200.59));  
  108.         hm.put("Tl"new Double(204.3833));  
  109.         hm.put("Pb"new Double(207.2));  
  110.         hm.put("Bi"new Double(208.9804));  
  111.         hm.put("Po"new Double(209));  
  112.         hm.put("At"new Double(210));  
  113.         hm.put("Rn"new Double(222));  
  114.         hm.put("Fr"new Double(223));  
  115.         hm.put("Ra"new Double(226));  
  116.         hm.put("Ac"new Double(227));  
  117.         hm.put("Pa"new Double(231.0359));  
  118.         hm.put("Th"new Double(232.0381));  
  119.         hm.put("Np"new Double(237));  
  120.         hm.put("U"new Double(238.0289));  
  121.         hm.put("Am"new Double(243));  
  122.         hm.put("Pu"new Double(244));  
  123.         hm.put("Cm"new Double(247));  
  124.         hm.put("Bk"new Double(247));  
  125.         hm.put("Cf"new Double(251));  
  126.         hm.put("Es"new Double(252));  
  127.         hm.put("Fm"new Double(257));  
  128.         hm.put("Md"new Double(258));  
  129.         hm.put("No"new Double(259));  
  130.         hm.put("Rf"new Double(261));  
  131.         hm.put("Lr"new Double(262));  
  132.         hm.put("Db"new Double(262));  
  133.         hm.put("Bh"new Double(264));  
  134.         hm.put("Sg"new Double(266));  
  135.         hm.put("Mt"new Double(268));  
  136.         hm.put("Rg"new Double(272));  
  137.         hm.put("Hs"new Double(277));  
  138.         hm.put("Ds"new Double(280));  
  139.     }  
  140.   
  141.     //This function is created temporary to test the functinality in BBD  
  142.     private void initializeDefaultreactPro() {  
  143.         reactpro = new Hashtable[2][];  
  144.         reactpro[0] = new Hashtable[1];  
  145.         reactpro[1] = new Hashtable[1];  
  146.         initializeReactPro();  
  147.     }  
  148.     //For initializing the reactPro Hashtable  
  149.     private void initializeReactPro() {  
  150.         for (int i = 0; i < reactpro.length; i++)  
  151.   
  152.         {  
  153.             for (int j = 0; j < reactpro[i].length; j++)  
  154.                 reactpro[i][j] = new Hashtable();  
  155.         }  
  156.     }  
  157.     private boolean contains(String args) {  
  158.         yesno = false;  
  159.         for (int i = 0; i < args.length(); i++) {  
  160.             if (args.charAt(i) == '(' || args.charAt(i) == '[') {  
  161.                 yesno = true;  
  162.                 break;  
  163.             }  
  164.         }  
  165.         return yesno;  
  166.     }  
  167.     public void accept() {  
  168.         try {  
  169.             System.out.println("Enter your expression");  
  170.             args = br.readLine();  
  171.             if (contains(args) == true)  
  172.                 getNoOfBrackets(args);  
  173.             reactpro[hmrpos][hmcpos].clear();  
  174.             calculateLength(args);  
  175.             matchElementToUpdate(reactpro[hmrpos][hmcpos], brentry);  
  176.             calculateMolWeight(hmrpos, hmcpos);  
  177.         } catch (Exception e1) {}  
  178.     }  
  179.   
  180.     //to calculate the no of Brackets appearing in the string  
  181.     private void getNoOfBrackets(String args) {  
  182.         for (int i = 0; i < args.length(); i++) {  
  183.             if (args.charAt(i) == '(' || args.charAt(i) == '[')  
  184.                 tempindex++;  
  185.         }  
  186.         templist = new Hashtable[tempindex];  
  187.   
  188.         tempindex = 0;  
  189.         initializeTempHash();  
  190.     }  
  191.     /*intialized the templist hashtable*/  
  192.     private void initializeTempHash() {  
  193.         for (int i = 0; i < templist.length; i++)  
  194.             templist[i] = new Hashtable();  
  195.     }  
  196.     private void calculateLength(String args) {  
  197.         fullLen = args.length();  
  198.         indexLen = fullLen - 1;  
  199.         readAtom(args);  
  200.     }  
  201.     //for calculating the next position within the String array.  
  202.     private int calculateNextPosition(int org) {  
  203.         int id = 0;  
  204.         if (org == fullLen - 1) {  
  205.             if (boxopen > 0 || roundopen > 0)  
  206.                 id = org;  
  207.             else  
  208.                 id = 0;  
  209.         } else if (org < fullLen - 1)  
  210.             id = org + 1;  
  211.         newid = id;  
  212.         return newid;  
  213.     }  
  214.     private void readAtom(String args) {  
  215.         for (int i = 0, nxtpos = 0; i < args.length(); i++) {  
  216.             temp = "";  
  217.             nxtpos = calculateNextPosition(i);  
  218.             if (Character.isUpperCase(args.charAt(i)) && Character.isUpperCase(args.charAt(indexLen - (indexLen - nxtpos)))) {  
  219.                 temp = String.valueOf(args.charAt(i));  
  220.                 addentry(1);  
  221.             } else if (Character.isUpperCase(args.charAt(i)) && (args.charAt(indexLen - (indexLen - nxtpos)) == '[' || args.charAt(indexLen - (indexLen - nxtpos)) == '(')) {  
  222.                 temp = String.valueOf(args.charAt(i));  
  223.                 addentry(1);  
  224.   
  225.                 if (args.charAt(indexLen - (indexLen - nxtpos)) == '(')  
  226.                     roundopen++;  
  227.                 else  
  228.                     boxopen++;  
  229.                 // suffix = 1;  
  230.                 i = readBrackets(args, indexLen - (indexLen - nxtpos));  
  231.             } else if (Character.isUpperCase(args.charAt(i)) && Character.isDigit(args.charAt(indexLen - (indexLen - nxtpos)))) {  
  232.   
  233.                 temp = String.valueOf(args.charAt(i));  
  234.                 //keyexists = reactpro[hmrpos][hmcpos].containsKey(temp);  
  235.                 i = IsDigit(args, indexLen - (indexLen - nxtpos));  
  236.                 addentry(digitvalue);  
  237.             } else if (Character.isUpperCase(args.charAt(i))) {  
  238.                 temp = String.valueOf(args.charAt(i));  
  239.                 i = readLower(args, i);  
  240.                 i = i - 1;  
  241.             } else if (args.charAt(i) == '[') {  
  242.                 boxopen++;  
  243.                 if (!brentry.isEmpty()) {  
  244.                     addToTempHash();  
  245.                     tempindex++;  
  246.                 }  
  247.                 i = readBrackets(args, i);  
  248.             } else if (args.charAt(i) == '(') {  
  249.                 roundopen++;  
  250.                 if (!brentry.isEmpty()) {  
  251.                     addToTempHash();  
  252.                     tempindex++;  
  253.                 }  
  254.                 i = readBrackets(args, i);  
  255.             }  
  256.         }  
  257.     }  
  258.     //This function is used for reading the elements having lower case letters and also for reading the digits  
  259.     private int readLower(String args, int idx) {  
  260.         for (int j = idx + 1, nxtpos = 0; j < args.length(); j++) {  
  261.             nxtpos = calculateNextPosition(j);  
  262.             if (Character.isLowerCase(args.charAt(j)) && (!(Character.isDigit(args.charAt(indexLen - (indexLen - nxtpos))) || Character.isLowerCase(args.charAt(indexLen - (indexLen -  
  263.                     nxtpos)))))) {  
  264.                 temp += String.valueOf(args.charAt(j));  
  265.                 if (roundopen == 0 && boxopen == 0)  
  266.                     addentry(1);  
  267.                 else {  
  268.                     if (!brentry.isEmpty()) {  
  269.                         addToTempHash();  
  270.                         tempindex++;  
  271.                     }  
  272.                     addentry(1);  
  273.                 }  
  274.             } else if (Character.isLowerCase(args.charAt(j)) && Character.isDigit(args.charAt(indexLen - (indexLen - nxtpos)))) {  
  275.                 temp += String.valueOf(args.charAt(j));  
  276.                 j = IsDigit(args, indexLen - (indexLen - nxtpos));  
  277.                 addentry(digitvalue);  
  278.             } else if (Character.isLowerCase(args.charAt(j)) && args.charAt(indexLen - (indexLen - nxtpos)) == '[') {  
  279.                 temp += String.valueOf(args.charAt(j));  
  280.                 addentry(1);  
  281.                 boxopen++;  
  282.                 //suffix=1;  
  283.                 addToTempHash();  
  284.                 tempindex++;  
  285.                 j = readBrackets(args, indexLen - (indexLen - nxtpos));  
  286.             } else if (Character.isLowerCase(args.charAt(j)) && args.charAt(indexLen - (indexLen - nxtpos)) == '(') {  
  287.                 temp += String.valueOf(args.charAt(j));  
  288.                 addentry(1);  
  289.                 roundopen++;  
  290.                 //suffix=1;  
  291.                 addToTempHash();  
  292.                 tempindex++;  
  293.                 j = readBrackets(args, indexLen - (indexLen - nxtpos));  
  294.             } else if (Character.isDigit(args.charAt(j)) && args.charAt(indexLen - (indexLen - nxtpos)) == ')') {  
  295.                 j = IsDigit(args, j);  
  296.                 if (roundopen > 1) {  
  297.                     keyexists = brentry.containsKey(temp);  
  298.                     if (keyexists) {  
  299.                         suffix = digitvalue;  
  300.                         j = IsDigit(args, indexLen - (indexLen - nxtpos) + 1);  
  301.                         digitvalue *= suffix;  
  302.                         suffix = 0;  
  303.                         addentry(digitvalue);  
  304.                         roundopen--;  
  305.                     } else  
  306.                         addentry(digitvalue);  
  307.                 } else  
  308.                     addentry(digitvalue);  
  309.             } else if (Character.isDigit(args.charAt(j)) && args.charAt(indexLen - (indexLen - nxtpos)) == ']') {  
  310.                 j = IsDigit(args, j);  
  311.                 if (boxopen > 1) {  
  312.                     keyexists = brentry.containsKey(temp);  
  313.                     if (keyexists) {  
  314.                         suffix = digitvalue;  
  315.                         j = IsDigit(args, indexLen - (indexLen - nxtpos) + 1);  
  316.                         digitvalue *= suffix;  
  317.                         suffix = 0;  
  318.                         addentry(digitvalue);  
  319.                         boxopen--;  
  320.                     } else  
  321.                         addentry(digitvalue);  
  322.                 } else  
  323.                     addentry(digitvalue);  
  324.             } else if (Character.isLowerCase(args.charAt(j))) {  
  325.                 temp += String.valueOf(args.charAt(j));  
  326.                 for (int k = j + 1, nxtpos1 = 0; j < args.length(); k++) {  
  327.                     nxtpos1 = calculateNextPosition(k);  
  328.                     if (Character.isLowerCase(args.charAt(k)) && !Character.isDigit(args.charAt(indexLen - (indexLen - nxtpos1)))) {  
  329.                         temp += String.valueOf(args.charAt(k));  
  330.                         addentry(1);  
  331.                     } else if (Character.isLowerCase(args.charAt(k)) && Character.isDigit(args.charAt(indexLen - (indexLen - nxtpos1)))) {  
  332.                         temp += String.valueOf(args.charAt(k));  
  333.                         k = IsDigit(args, k);  
  334.                         addentry(digitvalue);  
  335.                     } else if (Character.isLowerCase(args.charAt(k)) && args.charAt(indexLen - (indexLen - nxtpos1)) == '[') {  
  336.                         temp += String.valueOf(args.charAt(k));  
  337.                         addentry(1);  
  338.                         boxopen++;  
  339.                         k = readBrackets(args, indexLen - (indexLen - nxtpos1));  
  340.                     } else if (Character.isLowerCase(args.charAt(k)) && args.charAt(indexLen - (indexLen - nxtpos1)) == '(') {  
  341.                         temp += String.valueOf(args.charAt(k));  
  342.                         addentry(1);  
  343.                         roundopen++;  
  344.                         k = readBrackets(args, indexLen - (indexLen - nxtpos1));  
  345.                     } else if (Character.isDigit(args.charAt(k))) {  
  346.                         k = IsDigit(args, k);  
  347.                         addentry(digitvalue);  
  348.                     } else if (Character.isUpperCase(args.charAt(k))) {  
  349.                         j = k - 1;  
  350.                         break;  
  351.                     } else {  
  352.                         j = k - 1;  
  353.                         break;  
  354.                     }  
  355.                 }  
  356.             } else if (Character.isDigit(args.charAt(j)) && args.charAt(indexLen - (indexLen - nxtpos)) != ')') {  
  357.                 j = IsDigit(args, j);  
  358.                 addentry(digitvalue);  
  359.             } else if (Character.isUpperCase(args.charAt(j))) {  
  360.                 newid = j;  
  361.                 break;  
  362.             } else if (args.charAt(j) == '[' && args.charAt(indexLen - (indexLen - nxtpos)) == '(') {  
  363.                 boxopen++;  
  364.                 j = readBrackets(args, indexLen - (indexLen - nxtpos));  
  365.             } else if (args.charAt(j) == '[') {  
  366.                 boxopen++;  
  367.                 if (boxopen > 0) {  
  368.                     if (!brentry.isEmpty()) {  
  369.                         addToTempHash();  
  370.                         tempindex++;  
  371.                     }  
  372.                 }  
  373.                 j = readBrackets(args, j);  
  374.             } else if (args.charAt(j) == '(') {  
  375.                 roundopen++;  
  376.                 if (roundopen > 0) {  
  377.                     if (!brentry.isEmpty()) {  
  378.                         addToTempHash();  
  379.                         tempindex++;  
  380.                     }  
  381.                 }  
  382.                 j = readBrackets(args, j);  
  383.             } else if (args.charAt(j) == ')' && Character.isDigit(args.charAt(indexLen - (indexLen - nxtpos)))) {  
  384.                 roundopen--;  
  385.                 j = IsDigit(args, indexLen - (indexLen - nxtpos));  
  386.                 if (boxopen == 0 && roundopen == 0) {  
  387.                     if (!brentry.isEmpty()) {  
  388.                         updateentry(brentry, digitvalue);  
  389.                         tempindex = tempindex > 0 ? tempindex = tempindex - 1 : 0;  
  390.                         if (!templist[tempindex].isEmpty())  
  391.                             matchElementToUpdate(this.brentry, this.templist[tempindex]);  
  392.                         else {  
  393.                             //suffix=0;  
  394.                             addToReactPro();  
  395.                         }  
  396.                     } else  
  397.                         updateentry(reactpro[hmrpos][hmcpos], digitvalue);  
  398.                 } else {  
  399.                     updateentry(brentry, digitvalue);  
  400.                     tempindex = tempindex > 0 ? tempindex = tempindex - 1 : 0;  
  401.                     if (!templist[tempindex].isEmpty())  
  402.                         matchElementToUpdate(brentry, templist[tempindex]);  
  403.                     else {  
  404.                         //suffix=0;  
  405.                         addToReactPro();  
  406.                     }  
  407.                 }  
  408.             } else if (args.charAt(j) == ']' && Character.isDigit(args.charAt(indexLen - (indexLen - nxtpos)))) {  
  409.                 boxopen--;  
  410.                 j = IsDigit(args, indexLen - (indexLen - nxtpos));  
  411.                 if (boxopen == 0 && roundopen == 0) {  
  412.                     if (!brentry.isEmpty()) {  
  413.                         updateentry(brentry, digitvalue);  
  414.                         tempindex = tempindex > 0 ? tempindex = tempindex - 1 : 0;  
  415.                         if (!templist[tempindex].isEmpty())  
  416.                             matchElementToUpdate(brentry, templist[tempindex]);  
  417.                         else {  
  418.                             //suffix=0;  
  419.                             addToReactPro();  
  420.                         }  
  421.                     } else  
  422.                         updateentry(reactpro[hmrpos][hmcpos], digitvalue);  
  423.                 } else {  
  424.                     updateentry(brentry, digitvalue);  
  425.                     tempindex = tempindex > 0 ? tempindex = tempindex - 1 : 0;  
  426.                     if (!templist[tempindex].isEmpty())  
  427.                         matchElementToUpdate(this.brentry, this.templist[tempindex]);  
  428.                     else {  
  429.                         suffix = 0;  
  430.                         addToReactPro();  
  431.                     }  
  432.                 }  
  433.             } else if (args.charAt(j) == ')') {  
  434.                 roundopen--;  
  435.                 if (roundopen == 0 && boxopen == 0)  
  436.                     addToReactPro();  
  437.                 else {  
  438.                     tempindex = tempindex > 0 ? tempindex = tempindex - 1 : 0;  
  439.                     if (!templist[tempindex].isEmpty())  
  440.                         matchElementToUpdate(brentry, templist[tempindex]);  
  441.                     else {  
  442.                         suffix = 0;  
  443.                         addToReactPro();  
  444.                     }  
  445.                 }  
  446.             } else if (args.charAt(j) == ']') {  
  447.                 boxopen--;  
  448.                 if (roundopen == 0 && boxopen == 0)  
  449.                     addToReactPro();  
  450.                 else {  
  451.                     tempindex = tempindex > 0 ? tempindex = tempindex - 1 : 0;  
  452.                     if (!templist[tempindex].isEmpty())  
  453.                         matchElementToUpdate(brentry, templist[tempindex]);  
  454.                     else {  
  455.                         suffix = 0;  
  456.                         addToReactPro();  
  457.                     }  
  458.                 }  
  459.             }  
  460.             newid = j;  
  461.         }  
  462.         return newid;  
  463.     }  
  464.     //For reading the brackets expression   
  465.     private int readBrackets(String args, int idx) {  
  466.         for (int i = idx + 1, nxtpos = 0; i < args.length(); i++) {  
  467.             nxtpos = calculateNextPosition(i);  
  468.             if (Character.isUpperCase(args.charAt(i)) && Character.isUpperCase(args.charAt(indexLen - (indexLen - nxtpos)))) {  
  469.                 temp = String.valueOf(args.charAt(i));  
  470.                 addentry(1);  
  471.             } else if (Character.isUpperCase(args.charAt(i)) && args.charAt(indexLen - (indexLen - nxtpos)) == '[') {  
  472.                 temp = String.valueOf(args.charAt(i));  
  473.                 addentry(1);  
  474.                 addToTempHash();  
  475.                 tempindex++;  
  476.                 boxopen++;  
  477.                 //suffix=1;  
  478.                 i = readBrackets(args, indexLen - (indexLen - nxtpos));  
  479.             } else if (Character.isUpperCase(args.charAt(i)) && args.charAt(indexLen - (indexLen - nxtpos)) == '(') {  
  480.                 temp = String.valueOf(args.charAt(i));  
  481.                 addentry(1);  
  482.                 addToTempHash();  
  483.                 tempindex++;  
  484.                 roundopen++;  
  485.                 //suffix=1;  
  486.                 i = readBrackets(args, indexLen - (indexLen - nxtpos));  
  487.             } else if (Character.isUpperCase(args.charAt(i)) && args.charAt(indexLen - (indexLen - nxtpos)) == ')') {  
  488.                 temp = String.valueOf(args.charAt(i));  
  489.                 addentry(1);  
  490.                 i = readLower(args, i);  
  491.                 i = i - 1;  
  492.             } else if (Character.isUpperCase(args.charAt(i)) && args.charAt(indexLen - (indexLen - nxtpos)) == ']') {  
  493.                 temp = String.valueOf(args.charAt(i));  
  494.                 addentry(1);  
  495.                 i = readLower(args, i);  
  496.                 i = i - 1;  
  497.             } else if (args.charAt(i) == '(') {  
  498.                 roundopen++;  
  499.                 if (!brentry.isEmpty()) {  
  500.                     addToTempHash();  
  501.                     tempindex++;  
  502.                 }  
  503.                 continue;  
  504.             } else if (args.charAt(i) == '[') {  
  505.                 boxopen++;  
  506.                 if (!brentry.isEmpty()) {  
  507.                     addToTempHash();  
  508.                     tempindex++;  
  509.                 }  
  510.                 continue;  
  511.             } else if (Character.isUpperCase(args.charAt(i))) {  
  512.                 temp = String.valueOf(args.charAt(i));  
  513.                 i = readLower(args, i);  
  514.                 i = i - 1;  
  515.             }  
  516.             newid = i;  
  517.         }  
  518.         return newid;  
  519.     }  
  520.     //This function is used for adding elements in the respective hashmap considering whether any box brackets or round brackets  
  521.     //are open if not then add the elements to the reactpro hashmap  
  522.     private void addentry(int value) {  
  523.         if (boxopen > 0) {  
  524.             keyexists = brentry.containsKey(temp);  
  525.             if (roundopen > 0) {  
  526.                 if (keyexists == false)  
  527.                     brentry.put(temp, new Integer(value));  
  528.                 else  
  529.                     updateKeyValue(temp, value, brentry);  
  530.             } else {  
  531.                 if (keyexists == false)  
  532.                     brentry.put(temp, new Integer(value));  
  533.                 else  
  534.                     updateKeyValue(temp, value, brentry);  
  535.             }  
  536.         } else if (roundopen > 0) {  
  537.             keyexists = brentry.containsKey(temp);  
  538.             if (keyexists == false)  
  539.                 brentry.put(temp, new Integer(value));  
  540.             else  
  541.                 updateKeyValue(temp, value, brentry);  
  542.         } else {  
  543.             keyexists = reactpro[hmrpos][hmcpos].containsKey(temp);  
  544.             if (keyexists == false)  
  545.                 reactpro[hmrpos][hmcpos].put(temp, new Integer(value));  
  546.             else  
  547.                 updateKeyValue(temp, value, reactpro[hmrpos][hmcpos]);  
  548.         }  
  549.     }  
  550.   
  551.     //This function are called when the Box or Round brackets opens we are adding it to the templist hashtable  
  552.     //when anyother round or box bracket gets opened  
  553.     private void addToTempHash() {  
  554.         Enumeration keys = brentry.keys();  
  555.         while (keys.hasMoreElements()) {  
  556.             Object retrievedKey = keys.nextElement();  
  557.             templist[tempindex].put(retrievedKey, brentry.get(retrievedKey));  
  558.         }  
  559.         brentry.clear();  
  560.     }  
  561.     //For calculating the no of digits after the element.  
  562.     private int IsDigit(String args, int idx) {  
  563.         String variable = "";  
  564.         int newid2 = 0;  
  565.         for (int k = idx; k < fullLen; k++) {  
  566.             if (Character.isDigit(args.charAt(k))) {  
  567.                 variable += String.valueOf(args.charAt(k));  
  568.                 newid2 = k;  
  569.             } else {  
  570.                 newid2 = k - 1;  
  571.                 break;  
  572.             }  
  573.         }  
  574.         newid = newid2;  
  575.         if (variable.equals("") || variable.equals("0"))  
  576.             digitvalue = 1;  
  577.         else  
  578.             digitvalue = Integer.parseInt(variable);  
  579.         return newid;  
  580.     }  
  581.     //For updating the key value stored in the hashtable with that of the value specified after the round or box bracket  
  582.     protected void updateentry(Hashtable h, int value) {  
  583.         Enumeration keys = h.keys();  
  584.         while (keys.hasMoreElements()) {  
  585.             Object retrievedKey = keys.nextElement();  
  586.             h.put(retrievedKey, (Object) String.valueOf(Integer.parseInt(String.valueOf(h.get(retrievedKey))) * value));  
  587.         }  
  588.     }  
  589.     //This function is called when any box or round brackets gets closed we are updating the brentry hashtable  
  590.     //with that of the templist hashtable also checking before adding whether any key exists or not.If exists we are updating the key value   
  591.     // with that of brentry hashtable and templist hashtable. And also finally adding it to the final hashtable.  
  592.     public void matchElementToUpdate(Hashtable a, Hashtable b) {  
  593.         //If a hashmap is empty and the b is not empty then directly copy all the elements from hashmap b to hashmap a  
  594.         yesno = a.isEmpty() && !b.isEmpty() ? true : false;  
  595.         if (yesno == true)  
  596.   
  597.         {  
  598.             Enumeration keys = b.keys();  
  599.             while (keys.hasMoreElements()) {  
  600.                 Object retrievedKey = keys.nextElement();  
  601.                 a.put(retrievedKey, b.get(retrievedKey));  
  602.             }  
  603.         } else {  
  604.             Enumeration keys1 = a.keys();  
  605.             while (keys1.hasMoreElements()) {  
  606.                 Object retrievedKey1 = keys1.nextElement();  
  607.                 Enumeration keys2 = b.keys();  
  608.                 while (keys2.hasMoreElements()) {  
  609.                     Object retrievedKey2 = keys2.nextElement();  
  610.                     if (a.containsKey(retrievedKey2)) {  
  611.                         if (retrievedKey1.equals(retrievedKey2)) {  
  612.                             int result = Integer.parseInt(String.valueOf(a.get(retrievedKey1))) + Integer.parseInt(String.valueOf(b.get(retrievedKey2)));  
  613.                             a.put(retrievedKey1, (Object) String.valueOf(result));  
  614.                             b.remove(retrievedKey2);  
  615.                         }  
  616.                     } else {  
  617.                         a.put(retrievedKey2, b.get(retrievedKey2));  
  618.                         b.remove(retrievedKey2);  
  619.                     }  
  620.                 }  
  621.             }  
  622.         }  
  623.         b.clear();  
  624.     }  
  625.     private void addToReactPro() {  
  626.         Enumeration keys = brentry.keys();  
  627.         while (keys.hasMoreElements()) {  
  628.             Object retrievedKey = keys.nextElement();  
  629.             keyexists = reactpro[hmrpos][hmcpos].containsKey(retrievedKey);  
  630.             if (keyexists == false)  
  631.                 reactpro[hmrpos][hmcpos].put(retrievedKey, brentry.get(retrievedKey));  
  632.             else {  
  633.                 int value = Integer.parseInt(String.valueOf(brentry.get(retrievedKey)));  
  634.                 updateKeyValue(String.valueOf(retrievedKey), value, reactpro[hmrpos][hmcpos]);  
  635.             }  
  636.         }  
  637.         brentry.clear();  
  638.     }  
  639.     private void updateKeyValue(String key, int newVal, Hashtable h) {  
  640.         Enumeration keys = h.keys();  
  641.         while (keys.hasMoreElements()) {  
  642.             Object retrievedKey = keys.nextElement();  
  643.             if (retrievedKey.equals(key)) {  
  644.                 if (suffix != 0) {  
  645.                     h.put(retrievedKey, String.valueOf(Integer.parseInt(String.valueOf(h.get(retrievedKey))) * newVal));  
  646.                     suffix = 0;  
  647.                 } else  
  648.                     h.put(retrievedKey, String.valueOf(Integer.parseInt(String.valueOf(h.get(retrievedKey))) + newVal));  
  649.                 break;  
  650.             }  
  651.         }  
  652.     } //This function will return the atomic weight of that particular element which is stored in the hm hashmap  
  653.     //which is acting as a database in our case.  
  654.     private double getMolWeight(String key) {  
  655.         double value = 0;  
  656.         Enumeration keys = hm.keys();  
  657.         while (keys.hasMoreElements()) {  
  658.             Object retrievedKey = keys.nextElement();  
  659.             if (retrievedKey.equals(key)) {  
  660.                 value = Double.parseDouble(String.valueOf(hm.get(retrievedKey)));  
  661.                 break;  
  662.             }  
  663.         }  
  664.         return value;  
  665.     }  
  666.     //For calculating the molecular weight of the expression and for storing the molecular weight as per the compounds  
  667.     //in the mol_weight variable at a particular row and at a particular's column  
  668.     protected void calculateMolWeight(int rindex, int cindex) {  
  669.         finaloutput = 0;  
  670.         Enumeration keys = reactpro[rindex][cindex].keys();  
  671.         while (keys.hasMoreElements()) {  
  672.             Object retrievedKey = keys.nextElement();  
  673.             output = getMolWeight(String.valueOf(retrievedKey));  
  674.             finaloutput += output * Integer.parseInt(String.valueOf(reactpro[rindex][cindex].get(retrievedKey)));  
  675.         }  
  676.         System.out.println(finaloutput);  
  677.     }  
  678.     public static void main(String args[]) {  
  679.         ElementCalculation obj = new ElementCalculation();  
  680.         obj.setInitialValues();  
  681.         obj.accept();  
  682.     }  
  683. } 


Similar Articles