how to parse a line in java

Hello,
  How do I parse a line in java that is separated by "|" so that I get all the value including empty values

00000000556|name1|name1@yahoo.com|02/07/2014|456.43||||

I would like to get 9 elements, the last 4 is empty.. I tried the split command but it returns 5 elements and tokenizer command throw and exception for null values

String[] result = line.split(delims);


Thanks for the help
chand pbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
String[] result = line.split("\\|");

Open in new window

regex reserved character must be escaped
0
chand pbAuthor Commented:
Thanks I tried the split command and it does not return the correct arrays... It only return the element with data..

So if i have the string below.. the split command return an array of  5 elements

00000000556|name1|name1@yahoo.com|02/07/2014|456.43||||
0
awking00Commented:
I ran into this issue once before. The string.replaceAll function doesn't do a string replace but does a regular expression replace. Because regular expressions require the escape character, '\', for the pipe, but that character in itself needs to be escaped in Java, I never seemed to get the correct sequence of pipes and slashes. So I did it in a rather inelegant fashion, but it does work
            
String str = "0000000556|name1|name1@yahoo.com|09/07/2014|456.43||||";
		StringBuilder sb = new StringBuilder();
		char prev = '#';
		for (char c:str.toCharArray()) {
			if (c == '|' && prev == '|') {
				sb.append(" ");
			}
			sb.append(c);
			prev = c;
		}
		String[] result = sb.toString().split("\\|");

Open in new window

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

CEHJCommented:
Sorry - it should be the following (see API docs)

String[] result = line.split("\\|", -1);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chand pbAuthor Commented:
Thank you
0
awking00Commented:
Just want to say thanks to CEHJ for the far more elegant method. I never used the limit parameter with replaceAll before and didn't really know it existed until now. As often is the case, reading the API docs provided information on its proper usage :-)
0
CEHJCommented:
np :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.