Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# Read values in delimited string

M Wilson
Ranch Hand
Posts: 41
I want to assign a,b,c to first 3 values in the string delimited by "|" and a,b,c to next 3 values, and so on and so on for the length of String[] values
a=Y, b=100, c=200
a=N, b=200, c=400
a=N, b=500, c=600

The code below only returns a and b but not value for c and it only returns the first set over and over:
a=Y, b=100, c="", a=Y, b=100, c=""...
If I remove the for loop I can get values for a,b,c for first set only (Y,100,200). I don't undestand why this for loop does not work? Thank you.

String[] values = "Y|100|200|N|200|400|N|500|600";

for (int v = 0; v < values.length; v++)
{
String a = values[0];
String b = values[1];
String c = values[2];
// do something with a, b, c, then go get next values for a, b, c and do something...

}

fred rosenberger
lowercase baba
Bartender
Posts: 12087
29
Assuming you properly build an array of all the values, you have explicitly coded that a will ALWAYS be assigned hte first element of the array "values[0]".

You need to increment what you use each time for the various assignements... Something like

The first time through, v will be 0. you will assigne values[0] to a, then v gets incremented. values[1] will be assigned to b, and v gets incremented. values[2] will be assigned to c, and v gets incremented.

since you are incrementing v inside the loop you don't need to increment it in the 'for' statement.

Note this isn't perfect (and neither was your original code), because if the array doesn't have an even multiple of three elements, you'll get a arrayOutOfBounds exception (or something like that).

Ranch Hand
Posts: 277
Hi M,

I don't think you posted all your code because the code you posted above will not compile. You are trying to assign a String to an array of Strings, which would cause a compilation error. Could you please copy and paste all of your code?

If you are using the String.split method to create your String array, you must escape the '|' character because it is a special regular expression character.

M Wilson
Ranch Hand
Posts: 41
I wrote examples to make it easier to understand?
The code is:

String dataCovgValues = dataSheet.getCell(_column++, row).getContents();
final String[] covgValues = dataCovgValues.split("\\|");
for (int v = 0; v < CovgValues.length; ++v)
{
String x = covgValues[v++];
String y = covgValues[v++];
String z = covgValues[v++];
}

Yes the string will always be multiple of 3 elements.
This works until it reads the last value in the string, it throws java.lang.ArrayIndexOutOfBoundsException: 6 for the following example values of the string: "N|100|500|Y|200|300"

I tried for (int v = 0; v < covgValues.length + 1; v++) and it still throws same exception error. Can't figure out why it throws this error or how to fix.
Thank you.

M Wilson
Ranch Hand
Posts: 41
OOOps sorry "Bartender" I didn't see that you left the v++ out of the for loop.

pete stein
Bartender
Posts: 1561
edit: never mind! I had a brain-spasm or something.

M Wilson
Ranch Hand
Posts: 41
Hooray!!! It works!!!
Thank you very much!!!