Java Scanner Class Getting Input from user and file

Java Scanner class is used to take input from user and file.

Scanner class is defined as java.util.Scanner

It is defined as final class and extends Object class and implements Iterator<String> and Closeable interface.

Scanner class reads data in form of tokens and break token based on delimiter the default delimiter is white space.

The read token is converted to appropriate type based on next() method used.

To take input from keyboard following code can be used. System.in is standard input stream(keyboard)

Scanner class constructors

java.util.Scanner class has following constructors

Sr NoConstructors
1Scanner(File source)
Take a file as argument and Constructs a new Scanner that read from specified file.
2Scanner(File source, String charsetName)
Take File and String charsetName argument and construct Scanner object
3Scanner(InputStream source)
Constructs a new Scanner from InputStream.
4Scanner(InputStream source, String charsetName)
Constructs a new Scanner with InputStream and charserName.
5Scanner(Path source)
Take path of file to create constructor
6Scanner(Path source, String charsetName)
Constructs a new Scanner with path and charset name of file
7Scanner(Readable source)
Constructs a new Scanner based on readable
8Scanner(ReadableByteChannel source)
Constructs a new Scanner based on ReadableByteChannel
9Scanner(ReadableByteChannel source, String charsetName)
Constructs a new Scanner from ReadableByteChannel and charsetName
10Scanner(String source)
Constructs a new Scanner from String source.

Scanner Constructor with Standard Input Stream(keyboard). Used to take keyboard input from user in java

Scanner sc=new Scanner(System.in);

Scanner with String

Scanner sc1=new Scanner("String Data");

Scanner with File

Scanner sc2=new Scanner(new File("filenamewithextension");

Scanner class methods

Common methods in scanner class are


Sr NoReturn TypeMethod and Description
1Patterndelimiter()
Returns the Pattern this Scanner is currently using to match delimiters.
2booleanhasNext()
Returns true if this scanner has another token in input.
3booleanhasNext (Pattern pattern)
Returns true if the next token matches the specified pattern.
4booleanhasNext(String pattern)
Returns true if the next specified token matches the pattern
5booleanhasNextBigDecimal()
Returns true if the next token is BigDecimal
6booleanhasNextBigInteger()
Returns true if the next token is BigInteger.
7booleanhasNextBigInteger(int radix)
Returns true if the next token is BigInteger in the specified radix
8booleanhasNextBoolean()
Returns true if the next token is boolean value
9booleanhasNextByte()
Returns true if the next token is byte value
10booleanhasNextByte(int radix)
Returns true if the next token is byte value in the specified radix
11booleanhasNextDouble()
Returns true if the next token is double value
12booleanhasNextFloat()
Returns true if the next token is float value using the nextFloat() method.
13booleanhasNextInt()
Returns true if the next token is int value in the default radix using the nextInt() method.
14booleanhasNextInt(int radix)
Returns true if the next token is int value in the specified radix using the nextInt() method.
15booleanhasNextLine()
Returns true if there is another line in the input of this scanner.
16booleanhasNextLong()
Returns true if the next token is long value in the default radix using the nextLong() method.
17booleanhasNextLong(int radix)
Returns true if the next token is long value in the specified radix using the nextLong() method.
18booleanhasNextShort()
Returns true if the next token is a short value in the default radix using the nextShort() method.
19booleanhasNextShort(int radix)
Returns true if the next token is short value in the specified radix using the nextShort() method.
20Stringnext()
Finds and returns the next complete token from this scanner.
21Stringnext(Pattern pattern)
Returns the next token if it matches the specified pattern.
22Stringnext (String pattern)
Returns the next token if it matches the pattern constructed from the specified string.
23BigDecimalnextBigDecimal()
Scans the next token of the input as a BigDecimal.
24BigIntegernextBigInteger()
Scans the next token of the input as a BigInteger.
25BigIntegernextBigInteger(int radix)
Scans the next token of the input as a BigInteger.
26booleannextBoolean()
Scans the next token of the input into a boolean value and returns that value.
27bytenextByte()
Scans the next token of the input as a byte.
28bytenextByte(int radix)
Scans the next token of the input as a byte.
29doublenextDouble()
Scans the next token of the input as a double.
30floatnextFloat()
Scans the next token of the input as a float.
31intnextInt()
Scans the next token of the input as an int.
32intnextInt(int radix)
Scans the next token of the input as an int.
33longnextLong()
Scans the next token of the input as a long.
34longnextLong(int radix)
Scans the next token of the input as a long.
35shortnextShort()
Scans the next token of the input as a short.
36shortnextShort(int radix)
Scans the next token of the input as a short.
37intradix()
Returns this scanner’s default radix.
38Scannerreset()
Resets this scanner.
39Scannerskip(Pattern pattern)
Skips input that matches the specified pattern, ignoring delimiters.
40Scannerskip(String pattern)
Skips input that matches a pattern constructed from the specified string.
41StringtoString()
Returns the string representation of this Scanner.
42ScanneruseDelimiter(Pattern pattern)
Sets Scanner delimiting pattern to the specified pattern.
43ScanneruseDelimiter(String pattern)
Sets Scanner delimiting pattern to a pattern constructed from the specified String.
44ScanneruseRadix(int radix)
Sets Scanner default radix to the specified radix.

Scanner Class to Get User Input From Keyboard

Reading Data from String

Reading entire line using nextLine()

Reading Line by line from file