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.
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".
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 to a, then v gets incremented. values will be assigned to b, and v gets incremented. values 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).
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Please UseCodeTags next time to make your code more readable.
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.
SCJP 6 || SCWCD 5
Joined: May 23, 2010
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.
Joined: May 23, 2010
OOOps sorry "Bartender" I didn't see that you left the v++ out of the for loop.