Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

String.split() : Runtime Error in program !

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :
https://coderanch.com/t/533170/java-programmer-SCJP/certification/Page-Formatting-output-Generics-collections#2417911
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 \ ?
 
Sheriff
Posts: 22567
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic