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