Java’s Scanner Class

Ayana Bando
1 min readMay 8, 2021

--

What is it?

It is a simple text class that analyzes primitive types (int, double, float) and strings (String, char) using regular expressions. A Scanner breaks the input into tokens using a delimiter (comma, tab, whitespace); these tokens can then be converted into values of different types using the next()methods.

A Scanner breaks its input into tokens using a delimiter (comma, tab, whitespace) pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next()methods.’’

Basically, the Scanner class is used to take simple input to be used by an application as necessary.

The next(), and hasNext(), nextInt(), and hasNextInt() methods first skip any input that matches the delimiter pattern (comma, whitespace, etc), and then attempt to return the next token that was stored by Scanner.

This means that the call to next(), hasNext(), nextInt(), hasNextInt() might wait until data is available, which could be forever (depending on how the scanner is used). An application could stop working until new data is available.

The findInLine(), findWithinHorizon(), skip(), and findAll() methods operate independently of the delimiter pattern. These methods will attempt to match the specified pattern with no regard to delimiters in the input and thus can be used in special circumstances where delimiters are not relevant.

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Scanner.html

--

--

Ayana Bando
Ayana Bando

Written by Ayana Bando

Innovator and collaborator interested in continuing to grow and learn

No responses yet