简体   繁体   中英

Read and Split(Parse) data in java

I am trying to split some simple data from a .txt file. I have found some useful structures on the internet but it was not enough to split the data the way I wanted. I get a string like this:

{X:0.8940594 Y:0.6853521 Z:1.470214} 

And I want to transform it to like this;

0.8940594
0.6853521
1.470214

And then put them in a matrix in order X=[], Y=[], Z=[]; (the data is the coordinate of an 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 receive data like this:

{X:0.8940594
Y:0.6853521
Z:1.470214}

You can try using a regex similar to the following to match and capture the three numbers contained in each tuple:

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

Each quantity contained in parenthesis is a capture group, and is available after a match has taken place.

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;
}

You can use this regex -?\\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);

In your case you want to match the real number, you can check the Regex .

This code will solve your problem.

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);

Output->This List contains numbers:[0.8940594 , 0.6853521 , 1.470214]

How about this?

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);
    }
}

Your regex splits on whitespace, but does not remove the curly braces.

So instead of splitting on whitespace, you split on a class of characters: whitespace and curly braces.
The line with the regex then becomes:

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

Here is an ideone link with the full snippet.

After this, you'll want to split the resulting three lines on the : , and parse the righthand side. You can use Double.parseDouble for this purpose.

Personally, I would try to avoid long regex expressions; they are hard to debug.
It may be best to remove the curly braces first, then split the result on whitespace and colons. This is more lines of code, but it's more robust and easier to debug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM