• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

String Tokenizing doubt

 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


why output is 11 insteadof 10.........???
 
gaurav gupta sitm
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 " "
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic