-1

I get the following JsonNode with JsonNode

results = parentNode.get(GEOMETRY);

yields results: "marks (79.89 90.78)"

The problem is, I get the whole string "marks (79.89 90.78)" in results. But I need to fetch the doubles present inside () braces seperately.

Any idea as to how I can get the double numbers from this string?

I can use regular expression to get the numbers from the string(results string), but I want to know if there is any other workaround to do it.

user101
  • 21
  • 2

2 Answers2

0

If the structure is always the same, you could do something like this to parse the doubles out:

String fromJson = "marks (79.89 90.78)";
String[] split = fromJson.split(" ");
double a = Double.parseDouble(split[1].substring(1));
double b = Double.parseDouble(split[2].substring(0,split[2].length()-1));
System.out.println(a); // 79.89
System.out.println(b); // 90.78
Malt
  • 28,965
  • 9
  • 65
  • 105
0

You can use regular expression. It gives all the values which matches given pattern. You can change the pattern according your needs.

String s = "marks (79.89 90.78)";
    Pattern p = Pattern.compile("\\d+\\.\\d+");
    Matcher m = p.matcher(s);
    while(m.find()) {
        double d = Double.parseDouble(m.group());
    }
jnrdn0011
  • 417
  • 2
  • 13