Scanner class - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Scanner class

Description:

a Readable is a source of characters made available to callers of the read ... String input = '1 fish 2 fish red fish blue fish'; Scanner s ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 11
Provided by: patpa
Category:

less

Transcript and Presenter's Notes

Title: Scanner class


1
Scanner class

2
Scanner is a class for reading from devices
  • Readable
  • any object that implements the Readable()
    interface
  • a Readable is a source of characters made
    available to callers of the read method via a
    CharBuffer, usually with underlying get and put
    methods
  • ReadableByteChannel
  • a ReadableByteChannel object represents an open
    connection to a hardware device, a file, a
    network socket, or a program component that is
    capable of performing reading or writing bytes.
  • InputStream
  • InputStream is new InputString(System.in) //
    console
  • File
  • String

3
constructors
  • Scanner(InputStream source)
  • Scanner(InputStream source, String charsetName)
  • scan values from the specified input stream
  • Scanner(File source)
  • Scanner(File source, String charsetName)
  • scan values from the specified file
  • Scanner(String source)
  • scan values from the specified string
  • Scanner(Readable source)
  • scan values from the specified object (that
    implements the Readable interface)
  • Scanner(ReadableByteChannel source)
  • Scanner(ReadableByteChannel source, String
    charsetName)
  • scan values from the specified channel

4
java.util.Scanner
  • Scanner
  • extends Object
  • implements IteratorltStringgt
  • a text parser for primitives and strings
  • can use regular expressions
  • breaks its input into tokens using a delimiter
    pattern
  • by default matches whitespace
  • resulting String tokens may then be read
  • can perform automatic type conversion upon reading

5
read a number from System.in
  • Scanner sc new Scanner(System.in)

6
read long values from a file
  • Scanner sc
  • new Scanner( new File("myNumbers")
    )
  • while ( sc.hasNextLong() )
  • long aLong sc.nextLong()

7
use delimiters other than whitespace
  • String input "1 fish 2 fish red fish blue
    fish"Scanner s new Scanner(input).useDelim
    iter("\\sfish\\s")System.out.println(s.nextInt
    ()) System.out.println(s.nextInt())System.out.
    println(s.next())System.out.println(s.next())
    s.close()

output 1 2 red blue
8
use a regular expression to parse tokens
  • String input "1 fish 2 fish red fish blue
    fish"Scanner s new Scanner(input)s.findInLin
    e( "(\\d) fish (\\d) fish (\\w) fish
    (\\w)) MatchResult result s.match() for
    (int i1 iltresult.groupCount() i)
    System.out.println(result.group(i)) s.close()

output 1 2 red blue
9
after use, the Scanner must be closed
  • Scanner sc new Scanner(System.in)
  • // do something with the Scanner
  • in.close()

10
the end of this PowerPoint file
Hooray!
Write a Comment
User Comments (0)
About PowerShow.com