Package util

Class Scanner

java.lang.Object
util.Scanner

public class Scanner extends Object
Class for simple, fairly generic scanners (tokenizers). An input stream or a string is broken into tokens, which are returned one by one. The tokens that are recognized a fairly common set that fits a variety of applications.
Since:
2004.05.10
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    int
    current line number (determined by counting newline characters)
    static final int
    token: two character comparison operator
    static final int
    token: dash '--'
    static final int
    token: end of file
    static final int
    token: identifier or string
    static final int
    token: left arrow '<-'
    static final int
    token: number (floating point)
    static final int
    token: right arrow '->'
    int
    current token type
    current token value
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a scanner working on an input stream.
    Scanner(Reader reader)
    Create a scanner working on a reader.
    Scanner(String string)
    Create a scanner working on a string.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Close the scanner by closing the underlying reader.
    static final String
    format(String s, boolean quotes)
    Format a string so that it can be read as a token.
    void
    getChar(char c)
    Get the next token and compare it to an expected character.
    void
    Get the next token and check whether it is an identifier.
    void
    Get the next token and compare it to an expected identifier.
    void
    Get the next token and check whether it is an number.
    lno()
    Get a string stating the current line number.
    static void
    main(String[] args)
    Main function for testing basic functionality.
    int
    Get the next token of the input stream or the string.
    void
    nlToken(boolean flag)
    Set whether the newline character should be treated as a token or not.
    void
    Push back the last token into the input.
    void
    Push back the last token into the input.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • T_EOF

      public static final int T_EOF
      token: end of file
      See Also:
    • T_NUM

      public static final int T_NUM
      token: number (floating point)
      See Also:
    • T_ID

      public static final int T_ID
      token: identifier or string
      See Also:
    • T_RGT

      public static final int T_RGT
      token: right arrow '->'
      See Also:
    • T_LFT

      public static final int T_LFT
      token: left arrow '<-'
      See Also:
    • T_DASH

      public static final int T_DASH
      token: dash '--'
      See Also:
    • T_CMP

      public static final int T_CMP
      token: two character comparison operator
      See Also:
    • ttype

      public int ttype
      current token type
    • value

      public String value
      current token value
    • line

      public int line
      current line number (determined by counting newline characters)
  • Constructor Details

    • Scanner

      public Scanner(Reader reader)
      Create a scanner working on a reader.
      Parameters:
      reader - the reader to work on
      Since:
      2004.05.10 (Christian Borgelt)
    • Scanner

      public Scanner(String string)
      Create a scanner working on a string.
      Parameters:
      string - the string to scan
      Since:
      2004.05.10 (Christian Borgelt)
    • Scanner

      public Scanner(InputStream stream)
      Create a scanner working on an input stream.
      Parameters:
      stream - the input stream to scan
      Since:
      2004.05.10 (Christian Borgelt)
  • Method Details

    • lno

      public String lno()
      Get a string stating the current line number. Useful for error reporting.
      Returns:
      a string stating the current line number in the format "(line xxx)"
      Since:
      2004.05.20 (Christian Borgelt)
    • nlToken

      public void nlToken(boolean flag)
      Set whether the newline character should be treated as a token or not. By default it is treated as a whitespace character.
      Parameters:
      flag - if true, the newline character is treated as a token, otherwise as whitespace.
      Since:
      2005.03.01 (Christian Borgelt)
    • nextToken

      public int nextToken() throws IOException
      Get the next token of the input stream or the string.
      Returns:
      the code of the next token
      Throws:
      IOException - if an I/O error occurs or the scanner detects an error in the format of the stream
      Since:
      2004.05.10 (Christian Borgelt)
    • ungetToken

      public void ungetToken()
      Push back the last token into the input. Only the last token can be pushed back into the input; two consecutive calls have the same effect as a single call.
      Since:
      2004.05.10 (Christian Borgelt)
    • pushBack

      public void pushBack()
      Push back the last token into the input. Only the last token can be pushed back into the input; two consecutive calls have the same effect as a single call.
      Since:
      2004.05.10 (Christian Borgelt)
    • getChar

      public void getChar(char c) throws IOException
      Get the next token and compare it to an expected character.
      Parameters:
      c - the expected character
      Throws:
      IOException - if the expected character is not found
      Since:
      2005.02.21 (Christian Borgelt)
    • getID

      public void getID(String id) throws IOException
      Get the next token and compare it to an expected identifier.

      If null is passed for the expected identifier, it is only checked whether an identifier follows, while the value of this identifier is ignored.

      Parameters:
      id - the expected identifier
      Throws:
      IOException - if the expected identifier is not found
      Since:
      2005.02.21 (Christian Borgelt)
    • getID

      public void getID() throws IOException
      Get the next token and check whether it is an identifier.
      Throws:
      IOException - if the next token is not an identifier
      Since:
      2005.02.21 (Christian Borgelt)
    • getNumber

      public void getNumber() throws IOException
      Get the next token and check whether it is an number.
      Throws:
      IOException - if the next token is not a number
      Since:
      2005.02.21 (Christian Borgelt)
    • close

      public void close() throws IOException
      Close the scanner by closing the underlying reader.
      Throws:
      IOException - if an I/O error occurs
      Since:
      2007.02.08 (Christian Borgelt)
    • format

      public static final String format(String s, boolean quotes)
      Format a string so that it can be read as a token.
      Parameters:
      s - the string to format
      quotes - whether to add quotes
      Returns:
      the formatted string
      Since:
      2007.02.05 (Christian Borgelt)
    • main

      public static void main(String[] args)
      Main function for testing basic functionality.
      Parameters:
      args - the command line argument
      Since:
      2004.05.10 (Christian Borgelt)