• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Delimiter Scanner class  RSS feed

Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have the following code is a  snippet of code of book OCA8

it'is compile successufly and when i run it, it print the result:
"<X4>< we were><8><:-)><2C><1>"
really i don't undrestand why this print?
Posts: 13176
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is like a puzzle, isn't it? Like a Rubik's cube, I mean. Just like a Rubik's cube, you have to solve it one step at a time.

I suggest going to JShell, Java's REPL environment and experiment with each of these statements.
C:\ > jshell
|  Welcome to JShell -- Version 11.0.1
|  For an introduction type: /help intro

jshell> Scanner lexer = new Scanner("X4, we were||m8s & :-) 2C,1 THR,")
lexer ==> java.util.Scanner[delimiters=\p{javaWhitespace}+] ... \E][infinity string=\Q∞\E]

jshell> lexer.useDelimiter("[|,]")
$2 ==> java.util.Scanner[delimiters=[|,]][position=0][match valid=false][need input=false]
[source closed=false][skipped=false][group separator=\,][decimal separator=\.]
[positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=]
[NaN string=\QNaN\E][infinity string=\Q∞\E]

jshell> "<" + lexer.next("\\w*") + "><" + lexer.next() + ">"
$3 ==> "<X4>< we were>"

... and so on ...

Notice that you don't need to use System.out.println() to print an expression, just enter the expression directly and the REPL will do its thing to read, eval, and print the value of the expression.

It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!