Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes String.split()  : Runtime Error in program ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String.split()  : Runtime Error in program !" Watch "String.split()  : Runtime Error in program !" New topic
Author

String.split() : Runtime Error in program !

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

I want to save each of the words in path as a separate string in array [] demo. How can i do this (any other ways ?) and why i am i getting an exception ?

code :



runtime error :



PS : I need this for another program here :
http://www.coderanch.com/t/533170/java-programmer-SCJP/certification/Page-Formatting-output-Generics-collections#2417911

SCJP 6. Learning more now.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

\ is a special character in Java strings, so you have to escape it.

But it's also a special character in regular expressions. So you have to escape it again!

Try this:
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Matthew Brown wrote:\ is a special character in Java strings, so you have to escape it.

But it's also a special character in regular expressions. So you have to escape it again!

Try this:


Works ! that was blazing fast ! Can you also tell me what is happening ? I guess this is it :

\ : compiler expects \n , \t etc...so tell it there is none by putting \\
\\ : regex expects ... ???
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Matthew Brown wrote:


wait ... i had a small doubt - regex comes across the first \ what happens ?
point is why 4 \ ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Matthew already said it - you have to escape it twice.

A single backslash is "\\" in Java. That's what you had. But backslash is a special character for regular expressions as well. So you need to escape it in your regular expression as well. The regular expression then becomes \\ (two backslashes), but because you need to escape these two for the Java String you get four backslashes.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
And I think what was happening: the regular expression string had an escape character (single backslash) and nothing else; it blew up because it was not prepared to handle an escape character by itself as a regular expression string.

rc
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

is it like this : \\ to tell the compiler to take anything after them as it is. \\ for the regex. So what finally goes into split() in my CLASS file is just \\, right ?

I wonder what will happen if someone passes a string which uses only \ as separator to such a method. So , the person has to be aware of how he should type the strings.
Any remedy for this ?

PS : sorry for splitting hairs.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Rahul Sudip Bose wrote:I wonder what will happen if someone passes a string which uses only \ as separator to such a method. So , the person has to be aware of how he should type the strings.

Yes. The programmer must be aware that the argument to the split() method takes in a String that is a valid Regex pattern (similar to the argument you'd use for Pattern.compile())
The API documentation clearly states that the argument must be the 'delimiting regular expression' - String split


OCPJP 6
 
Consider Paul's rocket mass heater.
 
subject: String.split() : Runtime Error in program !
 
Similar Threads
replacing string with '\' character
split the string
replaceall() doubt
JavaHelp: could not create HelpSet
Page 569-571 : Formatting output AND making program in book (Generics and collections)