# Java: Summary - Scanner

## Reading values

The main use of Scanner is to read values from System.in or a file. Many of the Scanner methods fit a simple pattern: `nextX()` scans and returns a value of type X, except that X is the primitive type if there is one. But first the input can be tested to see if there is one with the boolean method `hasNextX()`, which returns true if something of type X can be found.

Possible values of X and the return type. Int (int) Double (double) BigDecimal (BigDecimal) BigInteger (BigInteger); Boolean (boolean) Byte (byte)

## Scanner methods

Assume: int i, double d, String s, boolean b, Scanner sc, and x stands for a type that should be clear from context.

Constructors
sc = | new Scanner(System.in); | Creates a Scanner which reads from System.in.
sc = | new Scanner(s); | Creates a Scanner which reads from String s.
Most common "next" input methods.
s = | sc.next() | Returns next "token", which is more or less a "word".
s = | sc.nextLine() | Returns next integer value.
i = | sc.nextInt() | Returns next integer value.
d = | sc.nextDouble() | Returns next double value.
Other input methods.
x = | sc.nextBigDecimal() | Returns next integer value.
x = | sc.nextBigInteger() | Returns next integer value.
x = | sc.nextBigInteger(radix) | Returns next integer value.
b = | sc.nextBoolean() | Returns next integer value.
x = | sc.nextByte() | Returns next integer value.
x = | sc.nextByte(radix) | Returns next integer value.
x = | sc.nextFloat() | Returns next integer value.
i = | sc.nextInt(radix) | Returns next integer value.
x = | sc.nextLong() | Returns next double value.
x = | sc.nextLong(radix) | Returns next double value.
x = | sc.nextShort() | Returns next double value.
x = | sc.nextShort(radix) | Returns next double value.
Use these tests for looping, optional elements, or error checking.

Copyleft 2005 Fred Swartz