Home » java » Java Scanner Class Getting Input from user and file

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 No Constructors
1 Scanner(File source)
Take a file as argument and Constructs a new Scanner that read from specified file.
2 Scanner(File source, String charsetName)
Take File and String charsetName argument and construct Scanner object
3 Scanner(InputStream source)
Constructs a new Scanner from InputStream.
4 Scanner(InputStream source, String charsetName)
Constructs a new Scanner with InputStream and charserName.
5 Scanner(Path source)
Take path of file to create constructor
6 Scanner(Path source, String charsetName)
Constructs a new Scanner with path and charset name of file
7 Scanner(Readable source)
Constructs a new Scanner based on readable
8 Scanner(ReadableByteChannel source)
Constructs a new Scanner based on ReadableByteChannel
9 Scanner(ReadableByteChannel source, String charsetName)
Constructs a new Scanner from ReadableByteChannel and charsetName
10 Scanner(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 No Return Type Method and Description
1 Pattern delimiter()
Returns the Pattern this Scanner is currently using to match delimiters.
2 boolean hasNext()
Returns true if this scanner has another token in input.
3 boolean hasNext (Pattern pattern)
Returns true if the next token matches the specified pattern.
4 boolean hasNext(String pattern)
Returns true if the next specified token matches the pattern
5 boolean hasNextBigDecimal()
Returns true if the next token is BigDecimal
6 boolean hasNextBigInteger()
Returns true if the next token is BigInteger.
7 boolean hasNextBigInteger(int radix)
Returns true if the next token is BigInteger in the specified radix
8 boolean hasNextBoolean()
Returns true if the next token is boolean value
9 boolean hasNextByte()
Returns true if the next token is byte value
10 boolean hasNextByte(int radix)
Returns true if the next token is byte value in the specified radix
11 boolean hasNextDouble()
Returns true if the next token is double value
12 boolean hasNextFloat()
Returns true if the next token is float value using the nextFloat() method.
13 boolean hasNextInt()
Returns true if the next token is int value in the default radix using the nextInt() method.
14 boolean hasNextInt(int radix)
Returns true if the next token is int value in the specified radix using the nextInt() method.
15 boolean hasNextLine()
Returns true if there is another line in the input of this scanner.
16 boolean hasNextLong()
Returns true if the next token is long value in the default radix using the nextLong() method.
17 boolean hasNextLong(int radix)
Returns true if the next token is long value in the specified radix using the nextLong() method.
18 boolean hasNextShort()
Returns true if the next token is a short value in the default radix using the nextShort() method.
19 boolean hasNextShort(int radix)
Returns true if the next token is short value in the specified radix using the nextShort() method.
20 String next()
Finds and returns the next complete token from this scanner.
21 String next(Pattern pattern)
Returns the next token if it matches the specified pattern.
22 String next (String pattern)
Returns the next token if it matches the pattern constructed from the specified string.
23 BigDecimal nextBigDecimal()
Scans the next token of the input as a BigDecimal.
24 BigInteger nextBigInteger()
Scans the next token of the input as a BigInteger.
25 BigInteger nextBigInteger(int radix)
Scans the next token of the input as a BigInteger.
26 boolean nextBoolean()
Scans the next token of the input into a boolean value and returns that value.
27 byte nextByte()
Scans the next token of the input as a byte.
28 byte nextByte(int radix)
Scans the next token of the input as a byte.
29 double nextDouble()
Scans the next token of the input as a double.
30 float nextFloat()
Scans the next token of the input as a float.
31 int nextInt()
Scans the next token of the input as an int.
32 int nextInt(int radix)
Scans the next token of the input as an int.
33 long nextLong()
Scans the next token of the input as a long.
34 long nextLong(int radix)
Scans the next token of the input as a long.
35 short nextShort()
Scans the next token of the input as a short.
36 short nextShort(int radix)
Scans the next token of the input as a short.
37 int radix()
Returns this scanner’s default radix.
38 Scanner reset()
Resets this scanner.
39 Scanner skip(Pattern pattern)
Skips input that matches the specified pattern, ignoring delimiters.
40 Scanner skip(String pattern)
Skips input that matches a pattern constructed from the specified string.
41 String toString()
Returns the string representation of this Scanner.
42 Scanner useDelimiter(Pattern pattern)
Sets Scanner delimiting pattern to the specified pattern.
43 Scanner useDelimiter(String pattern)
Sets Scanner delimiting pattern to a pattern constructed from the specified String.
44 Scanner useRadix(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

Please follow and like us:
error