Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

String is not behaving as expected if got from List

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I an facing one very strange issue while getting string value from a bean and split it using pattern :

I have to get the value and split it on the basis of "\n" presenting in String. I am doing as follows :

String attributeName = (attributeList.get(i).getName());
Pattern pattern = Pattern.compile("\n");
String[] formatArray1 = pattern.split(attributeName,0);

Problem : THe split is not working
But, If the same string value is hardcoded then split works perfectly fine as follows
:


String attributeName = "LOC_DATA_CURRENCY\nMX EQ (EUR)\nAAL\n Gross Loss";
Pattern pattern = Pattern.compile("\n");
String[] formatArray1 = pattern.split(attributeName,0);

This code is working fine and giving the length of array as 4.

I am not getting why it is not working as expected if getting from list .


Someone please assist. I got completely fed up and not finding any solution.....
 
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the split method works differently on an unknown string versus a known string, that can only mean that the two strings are different. It would be extremely easy to put in some debugging code to find out the actual value of the unknown string, so I suggest you should start by doing that.
 
soni lal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

Thanks for your reply..

I have already debugged the code and also put SOP to print the value getting from list.. Copied the same value (LOC_DATA_CURRENCY\nMX EQ (EUR)\nAAL\n Gross Loss) from console and hardcoded in String variable.

But still split is working fine with hardcoded String variable but, not with the value getting from list.... This is really strange behavior......


Please help...

 
Paul Clapham
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

soni lal wrote:Copied the same value (LOC_DATA_CURRENCY\nMX EQ (EUR)\nAAL\n Gross Loss) from console and hardcoded in String variable.



But the "\n" part you copied from the console is two characters, the backslash character and the "n" character. When you put that into a String literal it is interpreted as one character, the new-line character. So, like I said, the strings are different.

You should have seen that difference when you displayed the two strings. Perhaps your debugging didn't include that?
 
soni lal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

Thanks for the reply!!

Yes you are right .... while displaying both the string one is getting printed as complete value i.e "LOC_DATA_CURRENCY\nMX EQ (EUR)\nAAL\n Gross Loss" ( I think \n is getting treated as two separate characters not as single i.e line separator)
but,

if the same is hardcoded then the string is getting printed as :

LOC_DATA_CURRENCY
MX EQ (EUR)
AAL
Gross Loss


Now, what I have to do to split the string on basis of "\n" ...

Please help...
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds to me like the string to be split does not contain '\n' (the new line characters) but instead contains the two characters '\' followed by 'n'. In which case one needs to split based on "\\\\n" (the four '\' are needed because both Java and regex use '\' as an escape character) .
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic