• 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

K&B Chapter 6 - Scanner confusion

 
Greenhorn
Posts: 23
IntelliJ IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

I am reading K&B chapter-6. While encountering Scanner example, i am confused. Here is code snippet from K&B



java ScanIn "\d\d"
input: 1b2c335f456



I have two confusions here.

  • Where are we taking input? I thought calling next() will read the input. But this call is not present anywhere.
  • Why the last output is null in it?


  • Can someone explain please.

    Thanks,
    Samarbir
     
    Sheriff
    Posts: 9707
    43
    Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The documentation of Scanner.findInLine explains your questions

    JavaDoc wrote:Attempts to find the next occurrence of the specified pattern ignoring delimiters. If the pattern is found before the next line separator, the scanner advances past the input that matched and returns the string that matched the pattern. If no such pattern is detected in the input up to the next line separator, then null is returned and the scanner's position is unchanged. This method may block waiting for input that matches the pattern.



    So the findInLine method does the reading for input for you. It blocks to read input, whenever a match is found the pointer of the Scanner is moved forward and on the last iteration, since no match is found, the method returns null...
     
    reply
      Bookmark Topic Watch Topic
    • New Topic