aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Tokenizing doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Tokenizing doubt" Watch "String Tokenizing doubt" New topic
Author

String Tokenizing doubt

Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

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??


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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.


Mohamed Sanaulla | My Blog
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

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

Joined: Sep 08, 2007
Posts: 3071
    
  33

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

Joined: Aug 31, 2010
Posts: 394

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

Joined: Sep 08, 2007
Posts: 3071
    
  33

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

Joined: Feb 09, 2011
Posts: 6


why output is 11 insteadof 10.........???
gaurav gupta sitm
Greenhorn

Joined: Feb 09, 2011
Posts: 6
\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

Joined: Sep 08, 2007
Posts: 3071
    
  33

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

Joined: Jan 20, 2009
Posts: 172

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 " "


SCJD 6, OCPJP7, Baroque Potion, G+
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Tokenizing doubt