Java Scanner class is used to take input from users and file.
Scanner class is defined as java.util.Scanner
It is defined as a final class and extends the Object class and implements Iterator<String> and Closeable interface.
1 2 3 | public final class Scanner extends Object implements Iterator<String>, Closeable |
Scanner class reads data in form of tokens and break token based on the delimiter the default delimiter is white space.
The read token is converted to the appropriate type based on the next() method used.
To take input from the keyboard following code can be used. System.in
is standard input stream(keyboard)
1 2 | Scanner scanner=new Scanner(System.in); scanner.next(); |
other way to pass data during running of java file known as java command-line arguments
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");
Java 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package ebhor.io; import java.util.Scanner; public class UserInput { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter your Name"); String name = sc.next(); System.out.println("Enter your Age"); int age = sc.nextInt(); System.out.println("Name :" + name); System.out.println("Age :" + age); } } |
1 2 3 4 5 6 | Enter your Name Manish Enter your Age 23 Name :Manish Age :23 |
Java Scanner next(), nextInt(), nextFloat() examples
Reading Data from String
1 2 3 4 5 6 7 8 9 | public static void main(String[] args) { Scanner sc = new Scanner("Ram 25 20000.00"); System.out.println(sc.next()); System.out.println(sc.nextInt()); System.out.println(sc.nextFloat()); } |
Reading entire line using nextLine()
1 2 3 4 5 6 7 8 | public static void main(String[] args) { String s = new String("I've learned that you have to make careful choices because everything has an impact. " + "I've also learned that you can't please everyone in life," + " so please yourself and figure out what really matters"); Scanner sc = new Scanner(s); System.out.println(sc.nextLine()); } |
java scanner file reading
Reading Line by line from the file usnig java scanner class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class UserInput { public static void main(String[] args) { File f = new File("d://test.txt"); try { Scanner sc = new Scanner(f); while (sc.hasNextLine()) { System.out.println(sc.nextLine()); } } catch (FileNotFoundException ex) { System.out.println("File Not Found"); } } } |
1 2 3 4 5 6 7 | **Java Features** Java is Simple Java is Object Oriented Java is Robust Java is Secure Java is Multithreaded Java is Dynamic |