GeeCON Prague 2014*
The moose likes Java in General and the fly likes Read values in delimited string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Read values in delimited string" Watch "Read values in delimited string" New topic
Author

Read values in delimited string

M Wilson
Ranch Hand

Joined: May 23, 2010
Posts: 32
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

Joined: Oct 02, 2003
Posts: 11351
    
  16

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).


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

Hi M,

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
M Wilson
Ranch Hand

Joined: May 23, 2010
Posts: 32
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

Joined: May 23, 2010
Posts: 32
OOOps sorry "Bartender" I didn't see that you left the v++ out of the for loop.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
edit: never mind! I had a brain-spasm or something.
M Wilson
Ranch Hand

Joined: May 23, 2010
Posts: 32
Hooray!!! It works!!!
Thank you very much!!!
 
GeeCON Prague 2014
 
subject: Read values in delimited string