• 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

Scanner.stream()

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:. . . Pity that Scanner doesn't have a stream() method.

Winston

When Streams first came out, Cay Horstmann wrote the same sort of thing in a blog. I am not quite sure about that. Half of me thinks that Scanner ought to have a lines() method returning a Stream<String> (like BufferedReader). The other half thinks that Scanner is intended to tokenise its input, giving you anint then a double then a word, so what would its method look like? Maybe a tokens() method returning separate tokens into a Stream<String>, and maybe a lines() method as before?
 
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:The other half thinks that Scanner is intended to tokenise its input, giving you anint then a double then a word, so what would its method look like?


Well, by default Scanner is an Iterator to its tokens, which suggests to me that it could possibly look something like this:but since I'm new to all this stuff there may well be better ways - or I could be totally wrong.

Edit - Looking at the docs, it seems that it might be better to use StreamSupport.stream(Supplier, int, boolean), since Iterable's default method returns an early-binding Spliterator; but not sure how to do that yet, or whether it makes any difference in this case.

Winston
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:. . . Scanner is an Iterator . . . Winston

I had forgotten that. That is a good justification for giving it methods to create Streams.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Things changed in Java9.
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic