• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Scanners and Streams?

 
Marshal
Posts: 79151
377
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Almost exactly four years ago, Cay Horstmann was complaining that it wasn't possible to get a Stream out of a Scanner. Even confirmed Scannerophobes like Winston thought that shou‍ld be possible. In the Horstmann blog, somebody called Rémi said maybe Java9 would introduce a way to get a Stream from a Scanner. So I had a look about 4.00 o'clock today, and lo and behold, there are two/three methods: findAll() (overloaded) and tokens(). So I thought I would try them out:

java ScannerStreamDemo
Enter tokens: 0 to finish: Coderanch 1 2 69 is brilliant 4 ldhdl 123.45 0
244
Enter more tokens: 0 to finish: Coderanch 1 2 69 is brilliant 4 ldhdl 123.45 0
76

1+2+69+4=76; in the first case it was perceiving the 123 and the 45 as matching the regex supplied: 1+2+69+4+123+45=244.
If you don't have some means of stopping the Stream, it may turn into an infinite Stream.
There isn't a lines() method, but it shou‍ld be easy enough to get lines from tokens() with something like
useDelimiter("\r|\n|\r\n") or useDelimiter(System.lineSeparator())
 
author
Posts: 284
35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find tokens useful, and it shows that scanners haven't been entirely forgotten.

I haven't yet had a chance to put findAll to practical use, but try this in JShell to find words with four consecutive vowels:



Cheers,

Cay
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sure people will be q‑U‑E‑U‑E‑I‑n‑g up to try that bit of code. I shall try it myself in a minute.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does Louie appear twice? I see there aren't any words with five consecutive vowels, not even q‑U‑E‑U‑E‑I‑n‑g. I can see the logic of creating a Stream<String> from tokens() because, as Winston said in the other old thread, a Scanner IS‑AN Iterator<String>, and what you get from its next() method implemented from Iterator is a token, so the Stream supplies the same as you get from the method from Iterator.
I see there is a hasNext() method, but the remove

operation is not supported by this implementation of Iterator.

Obviously Iterator#remove() will be called an optional operation. It always struck me as peculiar that you can have interfaces with optional operations, but I suppose that is how you get Lists where you can add elements and Lists where you can't add elements.

Thank you for joining us to discuss this question
 
Cay Horstmann
author
Posts: 284
35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Louie occurs twice because the /usr/share/dict/words contains Louie and Louie's. However, my version does not contain queueing, only queuing. See also https://english.stackexchange.com/questions/60852/queueing-or-queuing
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cay Horstmann wrote:. . . /usr/share/dict/words contains Louie and Louie's.

Aaaaaaaaaaaaaaaaaaaaaah!

However, my version does not contain queueing . . .

The correct spelling is of course queueueing.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Even confirmed Scannerophobes like Winston thought that shou‍ld be possible.


I resemble that remark!
Hi all. Long time no Ranchee. Though I'd pop back to find some pearls of wisdom about Java 9, since I've just acquired a new laptop for a steal (150 quid for a 5-year old i5 HP, with 8 gig and and a fingerprint scanner) and want to install it.

Winston
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good to see you back
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Good to see you back


Indeed!
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome back Winston!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic