File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

.split String function no longer works

 
Paul Ngom
Ranch Hand
Posts: 355
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
H all,
I am encountering a problem while running this small piece of code.

The problem is: it does not run at all. No error message is displayed. It just returns to the command prompt when i run it.
Where am i wrong?
Thanks in advance.
Paul
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm certain it runs fine, it just doesn't output anything. Start with this question: How many elements does the "myWords" array have?
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Start with this question: How many elements does the "myWords" array have?

Hi Ulf,
Thanks a lot for your quick reply. The array myWorlds has five elements.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:

Start with this question: How many elements does the "myWords" array have?

Hi Ulf,
Thanks a lot for your quick reply. The array myWorlds has five elements.


I don't think Ulf asked you how many elements that you think your myWords should have. He is asking you how many elements the array that is returned actually have -- meaning print it out and confirm it.

And BTW, looking at your code, it should not return five elements.

Henry
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And BTW, looking at your code, it should not return five elements.

Hello Henry,
I am presently confused. I expect the program to print the following:
My
Home
Is
Being
Painted

but instead, i get no output.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, we understand that's what you expect. Have you tried Henry's suggestion of printing out how many elements the array actually has? If not, that's what you should start with.

The next step would be read closely the javadocs of the split method, paying particular attention to the parameter it takes.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:
Hello Henry,
I am presently confused. I expect the program to print the following:
<b>
My
Home
Is
Being
Painted
</b>
but instead, i get no output.


You get no output because the result from the split() method returns no elements. And there are two reasons for this...

1. The parameter to the split() method is a regular expression. And with regexes, the "." has special meaning.
2. There are actually overloaded split() methods. And with the split() method that you are using, it deletes trailing zero-length matches.

Hope this helps,
Henry
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry,
Thanks a lot for the explanation you gave.
I had to replace the "." by another string
and then applied the split function based on
that string. Now, all is well.
 
sai rama krishna
Ranch Hand
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is similar example from link http://www.java2novice.com/java_string_examples/Split_sample_code/

 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The tutorial you linked to is rather poor. The example is different from the OP's and does not help.

OP: What different String did you use? Did you follow Henry's hints? Have you looked at any regular expression tutorials? There is a good one in the Java Tutorials. If you go through that you should find a better way to split that String than to use the String#replace method. You probably have to replace \ by \\ when you move your regexes into String literals.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:
Thanks a lot for the explanation you gave.
I had to replace the "." by another string
and then applied the split function based on
that string. Now, all is well.



I don't know. If I was your client, and requested that you process my data -- and you required that I changed the data before you are able to process it, I would likely not call that "well".

In the real world, it is the application that has to change to deal with the changes in the requirements, and not the other way around.

Henry
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If I was your client, and requested that you process my data -- and you required that I changed the data before you are able to process it, I would likely not call that "well".

Henry,
You would not have to change anything in your data, all is done in code. Thanks again for your reply.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

OP: What different String did you use? Did you follow Henry's hints? Have you looked at any regular expression tutorials?

Campbell,
I used But now, i understand that i could have just used or Thanks for the link.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote: . . . But now, i understand that i could have just used . . . Thanks for the link.
Well done sorting it out; I think your second method which I have quoted would be the best solution.

And … “you're welcome”
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Ngom wrote:
I used


BTW, this won't work.... This will spit anything whose delimiter is either "." or "0" -- and not just ".".

Henry
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

BTW, this won't work.... This will spit anything whose delimiter is either "." or "0" -- and not just ".".

But it does work. Can you give me an example that contradicts it?
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will fail on strings which already contain a "0".
 
Winston Gutkowski
Bartender
Pie
Posts: 9501
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:But now, i understand that i could have just used...

Or indeed: (my personal favourite; I detest backslashes )

Winston
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It will fail on strings which already contain a "0".

You are right but he string i have provided contains no "0"s. :-)
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does also work. Thanks for sharing.
 
sai rama krishna
Ranch Hand
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"My.Home.Is.Being.Painted".split("[.]"); "
Does backslash work similar way as []. Can we use them interchangeably?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote: . . . Does backslash work similar way as []. Can we use them interchangeably?
No. No.


Go back up the thread to where I posted a Java Tutorials link.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:

It will fail on strings which already contain a "0".

You are right but he string i have provided contains no "0"s. :-)

Presumably the string that you want to split isn't always known beforehand. If it was, I wouldn't use split() at all, but just hard-code the result:

I hope you understood that the method of replacing the '.' with any other string, and then splitting on that string, is fundamentally flawed. It will always fail if the replacement string is already present in the string that you want to split - no matter what the replacement string is. It's also a strange and ugly workaround, only because you didn't know that split() takes a regular expression and that '.' has special meaning in a regular expression.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Presumably the string that you want to split isn't always known beforehand. If it was, I wouldn't use split() at all, but just hard-code the result:



Also, note that the answers provided in this topic assumes that the delimiter is known beforehand. If the delimiter is not known, and you need to create a regular expression to represent any literal string (disable all regex features), then you need to do this...



Henry
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That also works fine. Henry, thanks for sharing.

If the delimiter is not known, and you need to create a regular expression to represent any literal string (disable all regex features)

I do not get you well here. What do you mean by "disable all regex features"?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Henry means is that the argument passed from the quote method is read as an ordinary String "." rather than the regular expression which means any character.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As an example, let's say you have this string... "My\\Q.\\EHome\\Q.\\EIs\\Q.\\EBeing\\Q.\\EPainted". And of course, the delimiter is "\\Q.\\E". So, the results should be the same.

Question. What is the regular expression for the delimiter? As you know, there are special characters there -- and the delimiter as written as a regex is a literal dot... with the quote() method call, this is done for you (it will return a regular expression that will represent the delimiter as a literal string).

Henry
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As an example, let's say you have this string... "My\\Q.\\EHome\\Q.\\EIs\\Q.\\EBeing\\Q.\\EPainted". And of course, the delimiter is "\\Q.\\E". So, the results should be the same.

Question. What is the regular expression for the delimiter?

I cannot really tell but does the split correctly.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:

Question. What is the regular expression for the delimiter?

I cannot really tell but does the split correctly.



You didn't answer the question. The question was what is the regular expression for the delimiter? We know that calling the Pattern.quote() method returns the correct regular expression for the delimiter, in fact, that is why it was created.

But you don't know what it is... and thanks to the method, you don't really have to (but quite frankly, should be interested enough to look into it).

Henry
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You didn't answer the question. The question was what is the regular expression for the delimiter?

I said in my post that i cannot really tell.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:

You didn't answer the question. The question was what is the regular expression for the delimiter?

I said in my post that i cannot really tell.


 
Paul Ngom
Ranch Hand
Posts: 355
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following


the regex may be "\\Q\\Q.\\E\\\\E\\Q\\E"
This is rather confusing.
 
Naga Pranathi Chennuru
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even after replacing with \\. in split(), no output and array length is displayed. Can anyone please clarify on that?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please explain exactly what you are doing to get no output.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:Following


the regex may be "\\Q\\Q.\\E\\\\E\\Q\\E"
This is rather confusing.


Even for those that knows regular expressions very well, this take a bit of thinking... which is why it is a good idea, for those who are new to regular expressions, to just use the quote() method to make something complex a literal.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naga Pranathi Chennuru wrote: Even after replacing with \\. in split(), no output and array length is displayed. Can anyone please clarify on that?


Campbell Ritchie wrote:Welcome to the Ranch

Please explain exactly what you are doing to get no output.


Agreed. Considering that the OP solved the problem, this is likely something different. So, it would be a good idea to give us more details.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic