1 / 7

The infamous ‘end-of-line’ marker Scanner objects recognize ‘white space’ White space in java:

The infamous ‘end-of-line’ marker Scanner objects recognize ‘white space’ White space in java: a) space (an actual space) b) tab when you press the tab key, a special character is inserted in the document

thanh
Download Presentation

The infamous ‘end-of-line’ marker Scanner objects recognize ‘white space’ White space in java:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The infamous ‘end-of-line’ marker Scanner objects recognize ‘white space’ White space in java: a) space (an actual space) b) tab when you press the tab key, a special character is inserted in the document c) new line character When you press ‘enter’, a special character is inserted into the document

  2. But I can’t see those marks in a file! Correct – they are typically hidden. Open a word document and click the show paragraph icon (backward |P on the toolbar). You will see the hidden tab and end-of-line markers. Scanner objectsnextInt() nextDouble()nextLine() and so on. nextInt() starts at the current file marker, skips leading spaces until it hits a non-blank character, starts putting data into the input buffer until it reads white space. It is now finished. The buffer contains the data and it is sent back to the caller. The input file marker stays right there on the whitespace that it stopped on.

  3. Scanner objectsnextInt() nextDouble()nextLine() and so on. nextDouble() starts at the current file marker, skips leading spaces until it hits a non-blank character, starts putting data into the input buffer until it reads white space. It is now finished. The buffer contains the data and it is sent back to the caller. The input file marker stays right there on the whitespace that it stopped on. Hey – that’s the same as nextInt() ! That’s correct.

  4. All of these Scanner class methods do the same thing EXCEPT 1!! nextLine() starts at the input file marker, reads and fills the input buffer until it gets to a new-line-marker. The buffer contains the data and it is sent back to the caller. It stops there and throws the new line marker away, - unlike all the others who just stop and leave the input file marker sitting there waiting for the next data.

  5. What are the implications / consequences of this difference between nextLine() and all the others? Suppose you do option = console.nextInt(); then num1 = console.nextDouble(); Let E be ‘end-of-line’, T be tab, B a blank42Ewhere is the input file marker sitting? right before the E . nextInt() skips leading whitespace and reads non-blank characters until it reaches white space and stops.

  6. Now num1 = console.nextDouble();23.7Ewhere is the input file marker sitting? right before the E . nextDouble() skips leading whitespace and reads non-blank characters until it reaches white space and stops.Then you do console.nextLine()What happens? nextLine starts reading at the input file marker and reads anything until it gets to an end-of-line marker.So ----nextLine did not have to read very far to get to the end-of-line marker. It stops, throws the end of line marker away and now the input file marker is sitting at the beginning of the next line.

  7. So ----nextLine did not have to read very far to get to the end-of-line marker. It stops, throws the end of line marker away and now the input file marker is sitting at the beginning of the next line. OK, so when should I ‘Throw the end-of-line marker away’ ?? Answer – when you need to.You could do it indiscriminately, but you could also put wiggles around every line of code and not change the program – why would you do that?!?

More Related