Question

Forgive me if I'm already asking a question that has been answered but I'm not sure of the best way to do this being a beginner.

I have a string like the following, What would be the best way to convert this string so that I only have the doubles 3.4134388041,0.63117288 etc and I discard the rest?

Thanks!

Shading:Building:Detailed,
    39,                      !- Name
    ,                        !- Transmittance Schedule Name
    4,                       !- Number of Vertices
    3.4134388041,            !- Vertex 1 X-coordinate {m}
    0.63117288,              !- Vertex 1 Y-coordinate {m}
    2.2012378517,            !- Vertex 1 Z-coordinate {m}
    3.4134388041,            !- Vertex 2 X-coordinate {m}
    10.01517288,             !- Vertex 2 Y-coordinate {m}
    2.2012378517,            !- Vertex 2 Z-coordinate {m}
    2.9134388041,            !- Vertex 3 X-coordinate {m}
    10.01517288,             !- Vertex 3 Y-coordinate {m}
    2.2012378517,            !- Vertex 3 Z-coordinate {m}
    2.9134388041,            !- Vertex 4 X-coordinate {m}
    0.63117288,              !- Vertex 4 Y-coordinate {m}
    2.2012378517;            !- Vertex 4 Z-coordinate {m}
Was it helpful?

Solution

The following code should isolate every number which is not perfectly divisible by 1 within the string, printing them separately:

public static void main(String[] args)
{
    String text = ""; // string was too long to fit here for tidiness
    Matcher search = Pattern.compile("(?!=\\d\\.\\d\\.)([\\d.]+)").matcher(text);
    while (search.find())
    {
        double doub = Double.parseDouble(search.group(1));
        if (doub mod 1 != 0)
        {
            System.out.println(doub);
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top