1 / 5

Reading Input and Scanner Class

Reading Input and Scanner Class. Scanner Class Reading Input Reading for this class: L&L, 2.6. Reading Input. Programs generally need input on which to operate The Scanner class provides convenient methods for reading input values of various types

gstout
Download Presentation

Reading Input and Scanner Class

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. Reading Input and Scanner Class • Scanner Class • Reading Input • Reading for this class: L&L, 2.6

  2. Reading Input • Programs generally need input on which to operate • The Scanner class provides convenient methods for reading input values of various types • A Scanner object can be set up to read input from various sources, including from the user typing the values on the keyboard • Keyboard input is represented by the System.in object

  3. Reading Input • The following line creates a Scanner object that reads from the keyboard: Scanner scan = new Scanner (System.in); • The new operator creates the Scanner object • Once created, the Scanner object can be used to invoke various input methods, such as: answer = scan.nextLine();

  4. Reading Input • The Scanner class is part of the java.util class library, and must be imported into a program to be used • See Echo.java (page 91) • The nextLine method reads all of the input until the end of the line is found • The details of object creation and class libraries are discussed further in Chapter 3

  5. Input Tokens • Unless specified otherwise, white space is used to separate the elements (called tokens) of the input • White space includes space characters, tabs, new line characters • The next method of the Scanner class reads the next input token and returns it as a string • Methods such as nextInt and nextDouble read data of particular types • See GasMileage.java (page 92)

More Related