0

Write a program to parse the molecule and get the atoms count ? C6H12OH {C=6, O=1, H=13}

can u please help me with solution in java

24th May 2018, 11:23 AM
Pavan M
Pavan M - avatar
6 ответов
+ 7
Hmm, good, good. Let me try and see what I can come up with.
24th May 2018, 1:43 PM
Hatsy Rei
Hatsy Rei - avatar
+ 5
Please show your attempts at solving this problem.
24th May 2018, 1:24 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
class ParseMolecule {    public static void main(String s[])     {         System.out.println("The atoms count in the molecule C6H12OH is :" +getElementsMap("C6H12OH"));     } public static Map<String, Integer> getElementsMap(String molecule) { 
24th May 2018, 1:34 PM
Pavan M
Pavan M - avatar
0
Thank you so much. Hatsy
24th May 2018, 3:47 PM
Pavan M
Pavan M - avatar
0
import java.util.regex.*; import java.util.*; public class moleue_value import java.util.regex.*; import java.util.*; public class moleue_value { public static void main(String s[]) { String str = "C2H12oH"; Pattern p = Pattern.compile("(C|H|o)([0-9]*)"); Matcher m = p.matcher(str); Map <String, Integer> Atoms = new HashMap<>(); while (m.find()) { String element = m.group(1); Integer count = Integer.parseInt(m.group(2).equals("") ? "1" : m.group(2)); Atoms.put(element, count + (Atoms.get(element) == null ? 0 : Atoms.get(element))); } System.out.println(Atoms); } }
1st Jul 2019, 4:35 PM
MOHAN
MOHAN - avatar