Reading and sharing (analysis) of data in java

I am trying to break some simple data from a .txt file. I found some useful structures on the Internet, but this was not enough to separate the data the way I wanted. I get a line like this:

{X:0.8940594 Y:0.6853521 Z:1.470214} 

And I want to convert it like this:

0.8940594
0.6853521
1.470214

And then put them in the matrix in the order X = [], Y = [], Z = []; (data are the coordinates of the object)

Here is my code:

BufferedReader in = null; {
        try {
            in = new BufferedReader(new FileReader("file.txt"));
            String read = null;
            while ((read = in.readLine()) != null) {
                String[] splited = read.split("\\s+");
                for (String part : splited) {
                    System.out.println(part);
                }
            }
        } catch (IOException e) {
            System.out.println("There was a problem: " + e);
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (Exception e) {
            }
        }
    } 

What do I need to add to my code to get the data the way I want?

Right now, with this code, I get this data:

{X:0.8940594
Y:0.6853521
Z:1.470214}
+4
source share
5 answers

You can use this regular expression -?\d+\.\d+, for example:

String input = "{X:0.8940594 Y:0.6853521 Z:1.470214}";

Pattern pattern = Pattern.compile("-?\\d+\\.\\d+");
Matcher matcher = pattern.matcher(input);

List<String> result = new ArrayList<>();
while (matcher.find()) {
    result.add(matcher.group());
}
System.out.println(result);

, Regex.

+1

, , , :

{\s*X:(.*?)\s+Y:(.*?)\s+Z:(.*?)\s*}

, , , .

int size = 100;  // replace with actual size of your vectors/matrix
double[] A = new double[size];
double[] B = new double[size];
double[] C = new double[size];
String input = "{X:0.8940594 Y:0.6853521 Z:1.470214}";
String regex = "\\{\\s*X:(.*?)\\s+Y:(.*?)\\s+Z:(.*?)\\s*\\}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
int counter = 0;
while (m.find()) {
    A[counter] = Double.parseDouble(m.group(1));
    B[counter] = Double.parseDouble(m.group(2));
    C[counter] = Double.parseDouble(m.group(3));
    ++counter;
}
+4

.

String input = "{X:0.8940594 Y:0.6853521 Z:1.470214} ";
        String[] parts = input.split("(?<= )");
        List<String> output = new ArrayList();

        for (int i = 0; i < parts.length; i++) {
            //System.out.println("*" + i);
            //System.out.println(parts[i]);
            String[] part = parts[i].split("(?<=:)");
            String[] temp = part[1].split("}");
            output.add(temp[0]);
        }

        System.out.println("This List contains numbers:" + output);

- > : [0.8940594, 0.6853521, 1.470214]

+1

?

public class Test {
    public static void main(String[] args) {
        String s = "{X:0.8940594 Y:0.6853521 Z:1.470214}";
        String x = s.substring(s.indexOf("X:")+2, s.indexOf("Y:")-1);
        String y = s.substring(s.indexOf("Y:")+2, s.indexOf("Z:")-1);
        String z = s.substring(s.indexOf("Z:")+2, s.lastIndexOf("}"));
        System.out.println(x);
        System.out.println(y);
        System.out.println(z);
    }
}
0

, .

, : .
:

String[] splited = read.split("[\\s+\\{\\}]");

ideone .

After that, you will want to split the resulting three lines into :and analyze the right side. You can use Double.parseDouble for this purpose.

Personally, I try to avoid long regex expressions; they are difficult to debug.
It’s best to remove the curly braces first and then divide the result into spaces and colons. This is more lines of code, but it is more reliable and easier to debug.

0
source

Source: https://habr.com/ru/post/1673482/


All Articles