G
G
gibsonen2017-10-22 12:29:11
Java
gibsonen, 2017-10-22 12:29:11

What is the best way to read such numbers using java from a string?

There are lines like this: (3 numbers) (2 numbers) These lines are written according to the format, that is, there will be no different situations for the uncertainty of numbers. How can you count them as a number? Is there some built-in java method that parses strings like this? Or is it only possible to do it manually using, for example, regular expressions?
0.0-2.013896591961E-04-1.136868377216E-12
6.356909871101E-04-2.046363078989E-12

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Yakushev, 2017-10-26
@VYakushev

There is nothing built in. But with regular expressions can be easily implemented:

String src = "0.0-2.013896591961E-04-1.136868377216E-12";
Pattern p = Pattern.compile("\\d+\\.\\d+(E-\\d+)?");
Matcher m = p.matcher(src);
while (m.find()) {
    try {
        double value = Double.parseDouble(m.group(0));
        System.out.println(value);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

Here you can try - tpcg.io/cZrFFv

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question