0

I got a string that looks like this.

String line = "50464,"STRONACHLACHAR, PIER BUILDING AND PIER INCLUDING REVETMENT WALLS AND RAILINGS",C,04/05/2006,STIRLING,BUCHANAN";

If i want to split the string in 6 parts to put in an array with

String result = line.split(",");

i would have a problem since STRONACHLACHAR, PIER BUILDING AND PIER INCLUDING REVETMENT WALLS AND RAILINGS should be only one element because it's between "" but since there is also a komma there, it would be split there also. Also i wouldn't want the "'s there.

If i look for the position of the first ' and create a substring of that pos + 1 and then look for the other " in that substring and make a substring out of that.

Now i have 2 strings of which one has everything before the first " and one string that has the part that comes after the 2nd ", and the string value of STRONACHLACHAR, PIER BUILDING AND PIER INCLUDING REVETMENT WALLS AND RAILINGS. Then i can replace the komma there with nothing using replace(",", "");, maybe save the position of the komma to place it back once the string is split on komma's, but that aside. Next action would be to concatenate everyting together again so i get:

50464,STRONACHLACHAR PIER BUILDING AND PIER INCLUDING REVETMENT WALLS AND RAILINGS,C,04/05/2006,STIRLING,BUCHANAN

which can be succesfully split on komma's and i end up with an array of 6 elements i can work with.

In programming it would look like this. Left the part out on how to put the komma back.

    String line = "50464,\"STRONACHLACHAR, PIER BUILDING AND PIER INCLUDING REVETMENT WALLS AND RAILINGS\",C,04/05/2006,STIRLING,BUCHANAN";
    String end2= "";
    if(line.contains("\"")){
        int pos = line.indexOf("\"");
        String firstPart = line.substring(0, 6);
        String temp = line.substring(pos+1);
        int pos2 = temp.indexOf("\"");
        String secondPart = temp.substring(pos2+1);
        String temp2 = temp.substring(0, pos2-1);
        String temp3 = temp2.replace(",", "");
        String end = firstPart.concat(temp3);
        end2 = end.concat(secondPart);
    }
    String[] output = end2.split(",");
    for(int i = 0; i < output.length; i++){
        System.out.println(output[i] + " ");
    }

But what i am wondering about is: if this is good programming practice or am i thinking too complicated on how to do this? Since its a 1500 line file and every line would have to be checked. Even so there still might be other irregularities that need to be dealt with.

Btw, the purpose of this is that all lines should end up in an array of 6 elements, no more, no less.

What kind of parameters/thinking should i keep in mind to process a file/lines like this one?

Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191
WonderWorld
  • 956
  • 1
  • 8
  • 18

1 Answers1

2

Your data appears to be well-formed comma separated value (CSV) lines. Instead of tying yourself in knots, suggest reusing a library such as http://opencsv.sourceforge.net

chrisinmtown
  • 3,571
  • 3
  • 34
  • 43