• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String.split() : Runtime Error in program !

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
\ 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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:


wait ... i had a small doubt - regex comes across the first \ what happens ?
point is why 4 \ ?
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic