• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Read values in delimited string

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...

}
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
M Wilson
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OOOps sorry "Bartender" I didn't see that you left the v++ out of the for loop.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
edit: never mind! I had a brain-spasm or something.
 
M Wilson
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hooray!!! It works!!!
Thank you very much!!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic