views:

119

answers:

2

Consider the following String :

5|12345|value1|value2|value3|value4+5|777|value1|value2|value3|value4?5|777|value1|value2|value3|value4+

Here is how I want to split string, split it with + so I get this result :

myArray[0] = "5|12345|value1|value2|value3|value4";

myArray[1] = "5|777|value1|value2|value3|value4?5|777|value1|value2|value3|value4";

if string has doesn't contain char "?" split it with "|" and continue to part II, if string does contain "?" split it and for each part split it with "|" and continue to part II.

Here is part II :

myObject.setAttribute1(newString[0]);
...
myObject.setAttribute4(newString[3]);

Here what I've got so far :

private static String input = "5|12345|value1|value2|value3|value4+5|777|value1|value2|value3|value4?5|777|value1|value2|value3|value4+";

public void mapObject(String input){

String[] myArray = null;

    if (input.contains("+")) {
        myArray = input.split("+");
    } else {
        myArray = new String[1];
        myArray[0] = input;
    }

    for (int i = 0; i < myArray.length; i++) {

        String[] secondaryArray = null;
        String[] myObjectAttribute = null;

        if (myArray[i].contains("?")) {
            secondaryArray = temporaryString.myArray[i].split("?");

            for (String string : secondaryArray) {
                myObjectAttribute = string.split("\\|");
            }
        } else {
            myObjectAttribute = myArray[i].toString().split("\\|");
        }

        myObject.setAttribute1(myObjectAttribute[0]);
        ...
        myObject.setAttribute4(myObjectAttribute[3]);
                    System.out.println(myObject.toString());
}

Problem :

When I split myArray, going trough for with myArray[0], everything set up nice as it should.

Then comes the myArray[1], its split into two parts then the second part overrides the value of the first(how do I know that?). I've overridden toString() method of myObject, when I finish I print the set values so I know that it overrides it, does anybody know how can I fix this?

+3  A: 

I'm not quite sure what the intention is here, but in this snippet of code

secondaryArray = temporaryString.split("?");

for (String string : secondaryArray) {
    myObjectAttribute = string.split("\\|");
}

if secondaryArray has two elements after the split operation, you are iterating over each half and re-assigning myObjectAttribute to the output of string.split("\|") each time. It doesn't matter what is in the first element of secondaryArray, as after this code runs myObjectAttribute is going to contain the result of split("\\|") on the last element in the array.

Also, there is no point in calling .toString() on a String object as you do in temporaryString = myArray[i].toString().

matt b
+1: Exactly what I was going to say, beat me to it!
Ben S
@matt b what I'm trying to accomplish is because secondaryArray has 2 string values, so I want for first value to assign it to myObjectAttribute so I can extract and map my object attributes, and same thing for the second value of the array.
c0mrade
@matt b `as after this code runs myObjectAttribute is going to contain the result of split("\\|") on the last element in the array.` - yes I've noticed this as I said its overridden when I print myObject, how can I fix this, did you get my point?should I add more explanation? thank you
c0mrade
@matt b I've removed redundant temporaryString variable
c0mrade
+1  A: 

The code doesn't seem to be able to handle the possible expansion of strings in the secondary case. To make the code clearer, I would use a List rather than array.

private static String input = "5|12345|value1|value2|value3|value4+5|777|value1|value2|value3|value4?5|777|value1|value2|value3|value4+";

private void split(List<String> input, List<String> output, String split) {
    for (String s: input) {
          if (s.contains(split))
          {
             output.addAll(Arrays.asList(s.split(Pattern.quote(split)));
          }
          else
              output.add(s);
    }
}

public void mapObject(String input) {

List<String> inputSrings = new ArrayList<String>();
List<String> splitPlus = new ArrayList<String>();

inputStrings.add(input);

split(inputStrings, splitPlus);

List<String> splitQuest = new ArrayList<String>();
split(splitPlus, splitQuest, "?");

for (String s: splitQuest) {   
   // you can now set the attributes from the values in the list
   // splitPipe

   String[] attributes = s.split("\\|");
   myObject.setAttribute1(attributes[0]);
   ....
   myObject.setAttribute4(attributes[3]);
   System.out.println(myObject);
}

}

mdma
@mdma thank you for your answer but if it was up to me I'd also use List instead of array but I guess that is not an option .. sorry
c0mrade
how come lists not an option? If lists are really out, it's pretty easy to recode this to use arrays, it's just less code to use lists.
mdma
@mdma that is what I got so far I just need to make it work, if it was up to me this is really more elegant way to do it
c0mrade
It's not just about being elegant - this will make it work. The problem is that you don't know how the arrays will expand - some strings will split, others will not. Arrays are too static for this kind of work. It's going to be a nightmare trying to maintain this code. Please just try what I have posted. An additional advantage is that you can easily add additional split rules in future without having to rework loops and other logic that you have in the present solution.
mdma
@mdma I've tried your example like this http://pastebin.com/pmHSV7Yv and its working great, how can I map these values seperated by | to each myObject? currently its mapping the last one
c0mrade
please see my update. I now see what you want. I've changed so that the last split is done manually using String.split, and then each value from the array is added to myObject just like in your original code.
mdma
@mdma excellent worked like a charm I'll give it a go with arrays as well, but this is much better but its not up to me. I already gave you +1 . tnx
c0mrade
Great, really pleased to hear it's working. The requirement to use arrays seems strange - they're the wrong tool for the job. You can change the split() method to take a single array and return the array result. You can "resize" an array by copying it to a new array - you have to maintain a count of how many elements are in the array, or have code to ignore null values, and use "oversized" arrays.
mdma