How to extract double values from a string?
For example, s= "1.2+3.4*(3.2+2.3)-12.1/3.4*1.8+5.7"
How do extract the double values, and store it as variables?
How to extract double values from a string?
For example, s= "1.2+3.4*(3.2+2.3)-12.1/3.4*1.8+5.7"
How do extract the double values, and store it as variables?
You can do it with Pattern matching and grab matching portion out
Pattern p = Pattern.compile("[0-9]*\\.?[0-9]+");
Matcher m = p.matcher("1.2+3.4*(3.2+2.3)-12.1/3.4*1.8+5.7");
while (m.find()) {
System.out.println(m.group());
}
You can then collect all matched data to a List<Double>
or List<Float>
based on your requirement
To collect operators as well you need to add another pattern to look for as well see the ORing section
([0-9]*\.?[0-9]+)|(\+|-|\/|\(|\|\)|\*)
See