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
6 ответов
+ 7
Hmm, good, good. Let me try and see what I can come up with.
+ 5
Please show your attempts at solving this problem.
+ 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) {
0
Thank you so much. Hatsy
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);
}
}