This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Tokenizing doubt

 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source: OUKC (Oracle University Knowledge Center) mock exam question
Hi guys I have serious doubts about the escape options.


What is the result?

A. total: 3
B. total: 4
C. total: 7
D. total: 8
E. Compilation fails.
F. An exception is thrown at runtime.

The answer is D.
I chose compilation fails because I thought the legal escape was "\\d" and NOT "\d"
Which is valid for the real exam?? Can someone explain??
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried running compiling this code- It does give a compilation error. Not always that the mock exam questions are perfectly correct. It might be a printing mistake in the question otherwise or might be the answer is wrong.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:I tried running compiling this code- It does give a compilation error. Not always that the mock exam questions are perfectly correct. It might be a printing mistake in the question otherwise or might be the answer is wrong.

Thanks Mohammed, I have just found out from a reliable source that option D is correct.
"\d" is a valid regex metacharacter AND the real exam questions will treat it as "\d", NEITHER "\\d" NOR "\\\d" because the number of back or forward slashes depends ONLY on the underlying operating system.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because the number of back or forward slashes depends ONLY on the underlying operating system.


Can you explain about this?
And also let us know the reliable sources.

Looking at the fact that they have given a complete working code in the question one has to think in the compilation direction as well. Moreover I dont think the escape character depends on the underlying operating system.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:
because the number of back or forward slashes depends ONLY on the underlying operating system.


Can you explain about this?
And also let us know the reliable sources.

Looking at the fact that they have given a complete working code in the question one has to think in the compilation direction as well. Moreover I dont think the escape character depends on the underlying operating system.


Mohammed thank you for raising up the debate.

The reliable source is the book:
Sun Certified Programmer for Java 6 Study Guide. By: Kathy Sierra AND Bert Bates.
Page 503 Chapter 6: (Strings, I/O, Formatting and Parsing) it says quote:

"Note: Remember that to represent "\" in a string you may need to use the escape sequence "\\". Because of this, AND DEPENDING ON YOUR OS, your second argument might have to be ("\\d" or even "\\\\d".)"

As a computer programmer you need to understand that there are certain features that depends on the UNDERLYING OS(operating system).
That will make life(as A programmer) easier for you.

I know that there are some "jokers" in this forum, please "burn it in" that I am NOT one of them. Thank you.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ikpefua Jacob-Obinyan wrote:

The reliable source is the book:
Sun Certified Programmer for Java 6 Study Guide. By: Kathy Sierra AND Bert Bates.
Page 503 Chapter 6: (Strings, I/O, Formatting and Parsing) it says quote:

"Note: Remember that to represent "\" in a string you may need to use the escape sequence "\\". Because of this, AND DEPENDING ON YOUR OS, your second argument might have to be ("\\d" or even "\\\\d".)"



If you happen to read the NOTE which follows that- It clearly mentions that "\" has to be escaped. Otherwise the program runs into compilation error. If you did try running the code given earlier and passing "\d" as the command line argument- It does compile.

And also the depending on OS is due to the fact that there might be different escape sequences which different OS might/might not recognise in a String.

Ikpefua Jacob-Obinyan wrote:
As a computer programmer you need to understand that there are certain features that depends on the UNDERLYING OS(operating system).
That will make life(as A programmer) easier for you.

I knew this before.

Ikpefua Jacob-Obinyan wrote:
I know that there are some "jokers" in this forum, please "burn it in" that I am NOT one of them. Thank you.

No one claimed any one to be a joker and no one knows who is a Joker here . Just telling that "Reliable sources" would always lead to next question- "What is the reliable source?" So better to quote the sources.
 
gaurav gupta sitm
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why output is 11 insteadof 10.........???
 
gaurav gupta sitm
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
\d => digit
\D => non digit
\s => whitespace
\S => non whitespace
\w => word char
\W => non word

i know , how this work instead of \S.......
can anyone explain.......

Thanks
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The '\' in the string "\S" has to be escaped, otherwise the compiler will consider "\S" to be a escape sequence. So "\\S" has to be used where the extra "\" is used to escape the existing "\" in "\S".

And why do you think the output should be "10"? Is it not 11 the correct output?
 
David Byron
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gaurav gupta sitm wrote:
why output is 11 insteadof 10.........???

Split on 'x', sa[0] is ""
Split on '1', sa[1] is ""
Split on '2', sa[2] is ""
Split on '3', sa[3] is ""
Split on '4', sa[4] is ""
Split on 'y', sa[5] is " "
Split on '5', sa[6] is ""
Split on '6', sa[7] is ""
Split on 'z', sa[8] is " "
Split on '7', sa[9] is ""
Split on 'a', sa[10] is " "
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic