0

I have a large string that contains fixed value fields, this group of fields can repeat from 0 to 40 times. I would like to parse this list into an arraylist but I am not sure of the best way to do this?

The string data is as follows,

 CountryCode = 2 character
 StateProv   = 7 characters
 PostalCode  = 10 characters
 BuildingNum = 5 characters

There is no delimiter the pattern just repeats

any suggestion?

3
  • 1
    You probably need a map, not a list Commented Mar 7, 2011 at 17:05
  • 1
    Is the string data exactly like you are posting/ with a new line between each value? Commented Mar 7, 2011 at 17:07
  • give example input and expected result Commented Mar 7, 2011 at 17:28

2 Answers 2

5

I'd probably just read repeated substrings:

private static final int PATTERN_LENGTH = 24;
....

List<Location> list = new ArrayList<Location>();

if (text.length() % PATTERN_LENGTH != 0)
{
    // throw an exception of some description; you haven't got valid data
}
for (int start = 0; start < text.length(); start += PATTERN_LENGTH)
{
    list.add(Location.parse(text.substring(start, start + PATTERN_LENGTH));
}

(Or perform the parsing in the boody of the loop, or whatever... the main point is you've got the substring.)

Sign up to request clarification or add additional context in comments.

5 Comments

@Jon,could you provide with a example in ideone.com so that it could be handy for further reference.unable to understand this example here.
@Deepak, the example looks clear to me, can you expand on what is confusing?
@Deepak: Which precise part do you not understand?
why PATTERN_LENGTH is 24 and why if (text.length() % PATTERN_LENGTH != 0) and finally why a substring in final part.
@Deepak: PATTERN_LENGTH is 24 because that's 2+7+10+5 (i.e. the length of the whole pattern). The "if" block is to check that the text length is a multiple of the pattern length, i.e. we have a whole number of patterns. The substring is to take one instance of the pattern at a time.
0

I tried this using a character array. I am sure it can be optimized and I would like to see what changes you could apply to make it faster. Here is my test:

public class Test {

public static String bigString = "USARIZONA85123-123477777USWASHING78987-458711111USCOLORAD11111-111133333";

public List<String> getValues() {
    List<String> seperatedValues = new ArrayList<String>();

    char[] bigStringArray = bigString.toCharArray();
    char[] temp = new char[24];
    int j = 0;

    for (int i=1; i < bigStringArray.length+1; i++) {
        temp[j] = bigStringArray[i-1];
        j++;
        if (i%24 == 0) {
            seperatedValues.add(String.valueOf(temp));
            temp = new char[24];
            j = 0;
        }
    }

    return seperatedValues;
}

public static void main(String[] args) {
    Test test = new Test();
    System.out.println(test.getValues());
}

}

1 Comment

Why bother copying the string to an array to start with? Just use String.substring, which won't copy the characters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.