Methods
hasNext() → {boolean}
- Source:
Determines whether a non-whitespace element separated by whitespace exists in the buffer
Returns:
true if an element exists that is not whitespace in the buffer and is not EOF (End-of-File), false otherwise
- Type
- boolean
hasNextLine() → {boolean}
- Source:
Determines whether there are lines available in the buffer
Returns:
true if there are lines available in the buffer, false otherwise
- Type
- boolean
hasNextNumber() → {boolean}
- Source:
Determines whether the next non-whitespace element separated by whitespace is a number
Returns:
true if the next element is a number, false otherwise
- Type
- boolean
next() → (nullable) {string}
- Source:
Obtains the next non-whitespace element in the buffer separated by whitespace
Returns:
the next element in the buffer separated by whitespace or null if the buffer is empty
- Type
- string
nextLine() → (nullable) {string}
- Source:
Obtains the remaining parts of a line. If next or nextNumber is used to capture the last element of a line, this will return an empty string. To prevent this, the buffer must be flushed (see example).
Example
// input simulation where `System.in` contains the following:
// hello 1
// happy birthday
// incorrect way using the same input as above
var input = scanner(System.in);
var word = input.next(); // has the value "hello"
var value = input.nextNumber(); // will have the value 1
var line = input.nextLine(); // has the value of an empty string ("")
// correct way
var input = scanner(System.in);
var word = input.next(); // has the value "hello"
var value = input.nextNumber(); // has the the value 1
input.nextLine() // flushes the buffer (removes the newline character "\n")
var line = input.nextLine(); // correctly gets the next line (has the value "happy birthday")
Returns:
the remaining parts of a line or null if the buffer is empty
- Type
- string
nextNumber() → {number}
- Source:
Obtains the next non-whitespace element separated by whitespace in the buffer as a number
Throws:
-
Will throw an error if the next element is not a number
- Type
- TypeError
Returns:
the next element in the buffer as a number
- Type
- number
scanner(source) → {Scanner}
- Source:
Returns a Java-like Scanner that uses a source to parse
Parameters:
Name | Type | Description |
---|---|---|
source |
Array.<string> | string | the source to parse |
Throws:
-
Will throw an error if the given source is not of type Array.<string> or string
- Type
- TypeError
Returns:
the Scanner object
- Type
- Scanner
Type Definitions
Scanner
- Source:
Properties:
Name | Type | Description |
---|---|---|
next |
function | |
hasNext |
function | |
nextLine |
function | |
hasNextLine |
function | |
nextNumber |
function | |
hasNextNumber |
function |
Type:
- object