aspose file tools*
The moose likes Beginning Java and the fly likes .split String function no longer works Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ".split String function no longer works" Watch ".split String function no longer works" New topic
Author

.split String function no longer works

Paul Ngom
Ranch Hand

Joined: May 08, 2014
Posts: 317
    
    1
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


I am always surprised at the added knowledge i can get from others when i give my opinion on a topic.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42648
    
  65
I'm certain it runs fine, it just doesn't output anything. Start with this question: How many elements does the "myWords" array have?


Ping & DNS - my free Android networking tools app
Paul Ngom
Ranch Hand

Joined: May 08, 2014
Posts: 317
    
    1

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
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Ngom
Ranch Hand

Joined: May 08, 2014
Posts: 317
    
    1

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
Marshal

Joined: Mar 22, 2005
Posts: 42648
    
  65
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
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

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

Joined: May 08, 2014
Posts: 317
    
    1
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

Joined: May 29, 2009
Posts: 283
Here is similar example from link http://www.java2novice.com/java_string_examples/Split_sample_code/

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
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
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

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

Joined: May 08, 2014
Posts: 317
    
    1

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

Joined: May 08, 2014
Posts: 317
    
    1

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

Joined: Oct 13, 2005
Posts: 39885
    
  28
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
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40


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

Joined: May 08, 2014
Posts: 317
    
    1

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
Marshal

Joined: Mar 22, 2005
Posts: 42648
    
  65
It will fail on strings which already contain a "0".
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8250
    
  23

Paul Ngom wrote:But now, i understand that i could have just used...

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

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Paul Ngom
Ranch Hand

Joined: May 08, 2014
Posts: 317
    
    1

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

Joined: May 08, 2014
Posts: 317
    
    1
That does also work. Thanks for sharing.
sai rama krishna
Ranch Hand

Joined: May 29, 2009
Posts: 283
"My.Home.Is.Being.Painted".split("[.]"); "
Does backslash work similar way as []. Can we use them interchangeably?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
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

Joined: Aug 16, 2005
Posts: 14352
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

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

Joined: May 08, 2014
Posts: 317
    
    1

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

Joined: Oct 13, 2005
Posts: 39885
    
  28
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
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40


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

Joined: May 08, 2014
Posts: 317
    
    1

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
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

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

Joined: May 08, 2014
Posts: 317
    
    1

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
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

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

Joined: May 08, 2014
Posts: 317
    
    1
Following


the regex may be "\\Q\\Q.\\E\\\\E\\Q\\E"
This is rather confusing.
Naga Pranathi Chennuru
Greenhorn

Joined: Jun 26, 2014
Posts: 1
Even after replacing with \\. in split(), no output and array length is displayed. Can anyone please clarify on that?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Welcome to the Ranch

Please explain exactly what you are doing to get no output.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

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
 
jQuery in Action, 2nd edition
 
subject: .split String function no longer works