user3841581 user3841581 - 3 months ago 8
Java Question

using split based on multiple delimiters and different empty lines length

I have some .dat file that contains the following:

<D,E> 200 200 799 1220 No [<805,1380,Voltage,3,2>]
<A,C> 300 300 415 1230 Yes [<417,1340,Voltage,3,0><415,1230,Current,3,1>]
<D,B> 200 200 799 122 No [<80,137,Voltage,3,2>]
.
.


I would like to have the each line, the content of the third element; that is 200 on the first line and 300 on the second line and 200 on the third line. I would also like to have the 0 and the 1 (I would like to add them) on the second line and the 2 on the first and third lines.

I tried this

while ((line = file.readLine()) != null) {

if (line != null && !line.trim().isEmpty()) {
line = line.replace(" ", "|");
line = line.replace("||", "");
System.out.println(line);

String[] temp = line.split("|");
String temp1 = "";
String temp2 = "";

//System.out.println(temp[52]);
if (temp.length == 55) {
temp1 = temp[11] + temp[12] + temp[13];
temp2 = temp[52];


} else if (temp.length==52){
int len = temp.length;
temp1 = temp[11] + temp[12] + temp[13];
temp2 = temp[temp.length - 3];

}


}

For the first line it works, it also works when the temp.length has the same size; however, my line do not always have the same length. How can I split my line in a nice way so that I can have the elements I need.

Answer

You may first split the string with whitespaces (using .split("\\s+")) and then use a simple regex like ([0-9]+)> to extract chunks of digits before a >):

// Init the regex here
String rx = "([0-9]+)>";

// Then the part where you read the lines
String line = reader.readLine();
while (line != null) {
    String[] chunks = line.split("\\s+"); // Split with whitespace
    if (chunks.length > 2) { // chunks[2] = 200 / 300 values
        Matcher m = p.matcher(line); // Init Matcher to find all numbers at the end of >
        int val = 0;
        while (m.find()) {  // Find multiple occurrences numbers before >
            val += Integer.parseInt(m.group(1)); // Group 1 contains the number, summing up
        }
        res.add(chunks[2]);
        res.add(Integer.toString(val));
    }
    line = reader.readLine();
}
System.out.println(res); // => [200, 2, 300, 1, 200, 2]

See the IDEONE Java demo

Comments