Java Scanner
Input and output are the two most important operations of a computer. Input is the process if taking data in and output is the process of data out. The third part, the processing is the process on input data and generating the desired output.
Input is taken through various devices such as a mouse, keyboard, and touch. So to map this input from the keyboard to the machine-understandable form, there are some libraries in Java for the same. One of the libraries is Scanner which is a part of java.utill package.
Java Scanner reads text from the console and can parse the input into Java language primitive types and strings using regular expressions.
Features of Java Scanner
- Java Scanner class breaks the input into tokens using a delimiter which is whitespace by default.
- It is the easiest way to read input in Java program,
- By the help of Scanner in Java, we can get input from the user in primitive types such as int, long, double, byte, float, short, etc.
- The Java Scanner class extends Object class and implements Iterator and Closeable interfaces.
Shortcomings of Java Scanner
- The problem with Java Scanner arise when we are using any of the nextXXX() method, i.e. whenever the nextLine() method is called after anyone of the nextXXX() method then the method nextLine() does not read values from the console and it skips that step. So the input from the nextLine() which you want to read is ignored.
- Java Scanner is not Synchronized.
- Java Scanner is not Thread Safe
- Java Scanner has smaller buffer memory i.e. 1024 char
- As Java Scanner parses the input data, so it is a bit slower
Console Input Using Java Scanner
Java Scanner is a special Java Class that is used to take inputs from all kinds of input stream be it console, files, StringBuilder, StringBuffers to name a few.
'System.in' means console, we are creating a object of Java Scanner Class by the name of 'scanner' abd then we are telling Java compiler to accept input from the console, and save the accepted input into a String variable named 'inputString' .
- import java.util.Scanner;
- import static java.lang.System.out;
- /**
- * An example program to read a String from console input in Java
- */
- public class CSharpCorner
- {
- public static void main(String[] args)
- {
- out.print("Enter a string : ");
- Scanner scanner = new Scanner(System. in);
- String inputString = scanner. nextLine();
- out.println("String read from console is : \n"+inputString);
- }
- }
Java Scanner Constructors
- Scanner(File Source)
It constructs a new Scanner that produces values scanned from the specified file- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args) throws FileNotFoundException
- {
- Scanner sc = new Scanner(new File("example.txt"));
- while(sc.hasNext())
- {
- out.print(sc.next());
- }
- }
- }
- Scanner( File Source, String charsetName)
It constructs a new Scanner that produces values scanned from the specified file using the given decoding technique- import java.io.File;
- import java.io.FileNotFoundException;
- import java.nio.charset.StandardCharsets;
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args) throws FileNotFoundException
- {
- Scanner sc = new Scanner(new File("example.txt", StandardCharsets.US_ASCII.name()));
- while(sc.hasNext())
- {
- out.print(sc.next());
- }
- }
- }
- Scanner(InputStream source)
It constructs a new Scanner that produces values scanned from the specified input Stream.- import java.io.File;
- import static java.lang.System.out;
- import java.util.Scanner;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner myObj = new Scanner (System.in);
- out.println("Enter");
- String user = myObj.nextLine();
- out.print(user);
- }
- }
- Scanner(InputStream source, String charsetName)
It constructs a new Scanner that produces values scanned from the specified input stream using the given decoding technique.- import java.nio.charset.StandardCharsets;
- import java.io.File;
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner myObj = new Scanner (System.in, StandardCharsets.US_ASCII.name());
- out.println("Enter");
- String user = myObj.nextLine();
- out.print(user);
- }
- }
- Scanner( Readable source)
It constructs a new Scanner that produces values scanned from the specified source.- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args) throws FileNotFoundException
- {
- FileReader reader = new FileReader(new File("example.txt"));
- Scanner sc = new Scanner(reader);
- while(sc.hasNext())
- {
- out.print(sc.next());
- }
- }
- }
- Scanner(String source)
It constructs a new Scanner that produces values scanned from the specified string.- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- String reader = " CSharpCorner";
- Scanner sc = new Scanner(reader);
- while(sc.hasNext())
- {
- out.print(sc.next());
- }
- }
- }
- Scanner(ReadableByteChannel source)
It constructs a new Scanner that produces value scanned from the specified channel.- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.nio.charset.StandardCharsets;
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args) throws FileNotFoundException
- {
- Scanner scanner = new Scanner(new FileInputStream("example.txt").getChannel());
- out.println(scanner.nextLine());
- }
- }
- Scanner(ReadableByteChannel source, String charsetName)
It constructs a new Scanner that produces value scanned from the specified channel.- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.nio.charset.StandardCharsets;
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args) throws FileNotFoundException
- {
- Scanner scanner = new Scanner(new FileInputStream("example.txt").getChannel(),StandardCharsets.US_ASCII.name() );
- out.println(scanner.nextLine());
- }
- }
- Scanner(Path source)
It constructs a new Scanner that produces values scanned from the specified file.- import java.io.IOException;
- import java.nio.file.FileSystems;
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args) throws IOException
- {
- try (Scanner scanner = new Scanner(FileSystems.getDefault().getPath("lib","jdk.lib")))
- {
- out.println(scanner.nextLine());
- }
- }
- }
- Scanner(Path Source, String charsetName)
It constructs a new Scanner that produces values scanned from the specified file.- import java.io.IOException;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.FileSystems;
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args) throws IOException
- {
- try (Scanner scanner = new Scanner(FileSystems.getDefault().getPath("lib","jdk.lib"),StandardCharsets.US_ASCII.name()))
- {
- out.println(scanner.nextLine());
- }
- }
- }
Java Scanner Class Methods
1. Scanner.close()
Syntax
void close()
It is used to close the scanner
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CsharpCorner");
- scan.close();
- }
- }
2. Scanner.delimiter()
Syntax
pattern delimiter()
It is used to get the Pattern which the Scanner class is currently using to match delimiters.
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CsharpCorner");
- out.println(""+scan.nextLine());
- out.println(""+scan.delimiter());
- scan.close();
- }
- }
3. Scanner.findlnLine()
Syntax
1. String findlnLine(String pattern)
It is used to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
2. String findlnLine(Pattern pattern)
This is an inbuilt method which is used to find the next occurrence of the specified pattern ignoring delimiters
- import java.util.Scanner;
- import java.util.regex.Pattern;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CsharpCorner");
- out.println(scan.findInLine("sharp"));
- out.println(scan.findInLine(Pattern.compile("sharp")));
- scan.close();
- }
- }
4. Scanner.findWithinHorizon()
Syntax
1. String findWithinHorizon(Pattern pattern, int horizon)
This is an inbuilt method of Java Scanner class which is used to find the next occurrence of a specified pattern, it searches through the input up to the specified search horizon, ignoring delimiters.
2. String findWithinHorizon(String pattern, int horizon)
This is an inbuilt method of Java Scanner class which is used to find the next occurrence of a specified string, it searches through the input up to the specified search horizon, ignoring delimiters.
- import java.util.Scanner;
- import static java.lang.System.out;
- import java.util.regex.Pattern;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("1111, 2222, 33333, 45556");
- Pattern pattern = Pattern.compile("[0-9]+");
- while(scan.hasNext())
- {
- out.println(scan.findWithinHorizon(pattern, 18));
- }
- while(scan.hasNext())
- {
- out.println(scan.findWithinHorizon("[0-9]+", 10));
- }
- scan.close();
- }
- }
In the above code, in the first version we used pattern and in the second version we are using a String. The output of both will be exactly same.
5. Scanner.hasNext()
Syntax
1. boolean hasNext()
It returns true if this scanner has another token in its input. This method may be used for busy waiting i.e it may block while waiting for input to scan
2. boolean hasNext(String pattern)
It returns true if the next token matches the pattern constructed from the specified String
3. boolean hasNext(Pattern pattern)
It returns true if the next token matches the specified pattern.
- import java.util.Scanner;
- import java.util.regex.Pattern;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- while(scan.hasNext())
- {
- out.print(scan);
- }
- String pattern = "Sharp";
- if(scan.hasNext(pattern))
- {
- out.println("Pattern Found");
- }
- Pattern pattern1 = Pattern.compile("[[A-Za-z]*");
- while(scan.hasNext(pattern1))
- {
- out.println(scan.next());
- }
- scan.close();
- }
- }
6. Scanner.hasNextBigDecimal()
Syntax
boolean hasNextBigDecimal()
It is used to check if the next token in this scanner's input can be interpreted as BigDecimal or not. It returns true if the scanner's input can be interpreted as a BigDecimal, otherwise returns false.
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- if(scan.hasNextBigDecimal())
- {
- out.println("true");
- }
- else
- {
- out.println("false");
- }
- scan.close();
- }
- }
In the above code, since the text cannot be represented as BigDecimal hence the result will be "false"
7. Scanner.hasNextBigInteger()
Syntax
1. boolean hasNextBigInteger()
It is used to check if the next token in this scanner's input can be interpreted as BigInteger using the default radix or not. It returns true if the scanner's input can be interpreted as a BigInteger, otherwise returns false.
2. boolean hasNextBigInteger(int radix)
It is used to check if the next token in this scanner's input can be interpreted as BigInteger using the specified radix or not. It returns true if the scanner's input can be interpreted as a BigInteger, otherwise returns false.
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- if(scan.hasNextBigInteger())
- {
- out.println(scan.nextBigInteger());
- }
- if(scan.hasNextBigInteger(6))
- {
- out.println(scan.nextBigInteger());
- }
- scan.close();
- }
- }
In the above code, we are demonstarting the implemntation of both the versions of the method.
8. Scanner.hasNextBoolean()
Syntax
boolean hasNextBoolean()
It returns true if and only if this scanner's next token is a valid boolean value.
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- if(scan.hasNextBoolean())
- {
- out.println(scan.nextBigInteger());
- }
- scan.close();
- }
- }
In the above code, we are checking the given scanner input has boolean value in it
9. Scanner.hasNextByte()
Syntax
1. boolean hasNextByte()
It is used to check if the next token in this scanner's input can be interpreted as Byte using the default radix or not. It returns true if the scanner's input can be interpreted as a Byte, otherwise returns false.
2. boolean hasNextByte(int radix)
It is used to check if the next token in this scanner's input can be interpreted as Byte using the specified radix or not. It returns true if the scanner's input can be interpreted as a Byte, otherwise returns false.
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- if(scan.hasNextByte())
- {
- out.println(scan.nextByte());
- }
- if(scan.hasNextByte(6))
- {
- out.println(scan.nextByte());
- }
- scan.close();
- }
- }
10. Scanner.hasNextDouble()
Syntax
boolean hasNextDouble()
It returns true if and only if this scanner's next token is a valid double value
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- if(scan.hasNextDouble())
- {
- out.println(scan.nextDouble());
- }
- scan.close();
- }
- }
11. Scanner.hasNextFloat()
Syntax
boolean hasNextFloat()
It returns true if and only if this scanner's next token is a valid float value
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- if(scan.hasNextFloat())
- {
- out.println(scan.nextFloat());
- }
- scan.close();
- }
- }
In the above code, we are finding whether the next token is float value or not.
12. Scanner.hasNextInt()
Syntax
1. boolean hasNextInt()
It is used to check if the next token in this scanner's input can be interpreted as Integer using the default radix or not. It returns true if the scanner's input can be interpreted as an Integer, otherwise returns false.
2. boolean hasNextInt(int radix)
It is used to check if the next token in this scanner's input can be interpreted as Integer using the specified radix or not. It returns true if the scanner's input can be interpreted as an Integer, otherwise returns false.
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- if(scan.hasNextInt())
- {
- out.println(scan.nextInt());
- }
- if(scan.hasNextInt(6))
- {
- out.println(scan.nextInt());
- }
- scan.close();
- }
- }
In the above code, we are demonstrating the use of both the vesions of the method i.e with default radix and user-defined radix
13. Scanner.hasNextLine()
Syntax
boolean hasNextLine()
It returns true if and only if this scanner has another line of input
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.hasNextLine());
- scan.close();
- }
- }
In the above code, we are checking if the scanner has another input, and if yes then we print that
14. Scanner..hasNextLong()
Syntax
1. boolean hasNextLong()
It is used to check if the next token in this scanner's input can be interpreted as Long using the default radix or not. It returns true if the scanner's input can be interpreted as a Long, otherwise returns false.
2. boolean hasNextLong(int radix)
It is used to check if the next token in this scanner's input can be interpreted as Long using the specified radix or not. It returns true if the scanner's input can be interpreted as a Long, otherwise returns false.
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- if(scan.hasNextLong())
- {
- out.println(scan.nextLong());
- }
- if(scan.hasNextLong(6))
- {
- out.println(scan.nextLong());
- }
- scan.close();
- }
- }
In the above code, we are implementing both versions of the method i.e with deafult radix and with user defined radix.
15. Scanner.hasNextshort()
Syntax
1. boolean hasNextShort()
It is used to check if the next token in this scanner's input can be interpreted as Short using the default radix or not. It returns true if the scanner's input can be interpreted as a Short, otherwise returns false.
2. boolean hasNextShort(int radix)
It is used to check if the next token in this scanner's input can be interpreted as Short using the specified radix or not. It returns true if the scanner's input can be interpreted as a short, otherwise returns false.
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- if(scan.hasNextShort())
- {
- out.println(scan.nextShort());
- }
- if(scan.hasNextShort(6))
- {
- out.println(scan.nextShort());
- }
- scan.close();
- }
- }
In the above code, we are implementing both versions of the method i.e with deafult radix and with user defined radix.
16. Scanner.ioException()
Syntax
IOException ioException()
It returns the last exception thrown by this scanner's readable
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.ioException());
- }
- }
In the above code, since there no IOException generated hence, the output wil be "null"
17. Scanner.locale()
Syntax
Locale locale()
It returns the scanner's locale
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.locale());
- }
- }
18. Scanner.match()
Syntax
MatchResult match()
It returns a match result for the last match operation
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.print(scan.hasNext("Csharp"));
- out.println(scan.match());
- }
- }
19. Scanner.next()
Syntax
It returns the next token if it matches the specified pattern.
- import java.util.Scanner;
- import java.util.regex.Pattern;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.next());
- String pattern = "[0-9]";
- out.println(scan.next(pattern));
- out.println(scan.next(Pattern.compile("[0-9]")));
- }
- }
In the above code, we have implemented all the 3 versions of the method.
20. Scanner.nextBigDecimal()
Syntax
BigDecimal nextBigDecimal()
It returns the BigDecimal Scanned from the output.
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.nextBigDecimal());
- }
- }
In the above code, we will be printing the next BigDecimal value
21. Scanner.nextBigInteger()
Syntax
1. BigInteger nextBigInteger()
It scans the next token of the input as a BigInteger in the default radix
2. BigInteger nextBigInteger(int radix)
It scans the next token of the input as a BigInteger in the specified radix
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.nextBigInteger());
- out.println(scan.nextBigInteger(6));
- }
- }
22. Scanner.nextBoolean()
Syntax
boolean nextBoolean()
It returns the boolean Scanned from the output.
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.nextBoolean());
- }
- }
23. Scanner.nextByte()
Syntax
1. byte nextByte()
It scans the next token of the input as a byte in the default radix
2. byte nextByte(int radix)
It scans the next token of the input as a byte in the specified radix
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.nextByte());
- out.println(scan.nextByte(6));
- }
- }
In the above code, we will be printing the next byte value
24. Scanner.nextDouble()
Syntax
boolean nextDouble()
It returns the double Scanned from the output.
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.nextDouble());
- }
- }
In the above code, we will be printing the next Double value
25. Scanner.nextFloat()
Syntax
boolean nextFloat()
It returns the float Scanned from the output.
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.nextFloat());
- }
- }
In the above code, we will be printing the next Float value
26. Scanner.nextInt()
Syntax
1. boolean nextInt()
It scans the next token of the input as an Int in the default radix
2. boolean nextInt(int radix)
It scans the next token of the input as an Int in the specified radix
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.nextInt());
- out.println(scan.nextInt(6));
- }
- }
27. Scanner.nextLine()
Syntax
String nextLine()
It returns the String Scanned from the output.
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.nextLine());
- }
- }
In the above code, we are printing the next String value
28. Scanner.nextLong()
Syntax
1. boolean nextLong()
It scans the next token of the input as a Long in the default radix
2. boolean nextLong(int radix)
It scans the next token of the input as a Long in the specified radix
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.nextLong());
- out.println(scan.nextLong(6));
- }
- }
29. Scanner.nextShort()
Syntax
1. boolean nextShort()
It scans the next token of the input as a Short in the default radix
2. boolean nextShort(int radix)
It scans the next token of the input as a Short in the specified radix
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.nextShort());
- out.println(scan.nextShort(6));
- }
- }
In the above code, we are printing the next Short value.
30. Scanner.radix()
Syntax
int radix()
It returns the default radix of the Scanner.
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.radix());
- }
- }
In the above, we will get 10 as the default radix
31. Scanner.remove()
Syntax
void remove()
It is used when the remove operation is not supported by this implementation of Iterator
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.next());
- scan.remove();
- out.println(scan.next());
- }
- }
In the above we are basically removing the content of Scanner
32. Scanner. reset()
Syntax
Scanner reset()
It is used to reset the Scanner in use.
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.next());
- out.println(scan.reset());
- }
- }
In the above code, we are resetting the contents of the Scanner
33. Scanner.skip()
Syntax
1. Scanner skip(Pattern pattern)
It is a method which skips input that matches the specified pattern, ignoring delimiters.
2. Scanner skip(String pattern)
It is a method which skips inout that matches a pattern constructed from the specified string
- import java.util.Scanner;
- import java.util.regex.Pattern;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.next());
- String pattern = "[A-Z]*";
- while(scan.hasNext())
- {
- scan.skip(pattern);
- out.println(scan.next());
- }
- Scanner scan1 = new Scanner("CSharpCorner");
- scan1.skip(Pattern.compile("...ar"));
- out.println(scan1.nextLine());
- }
- }
34. Scanner.toString()
Syntax
String toString()
It returns the String representation of this Scanner
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner");
- out.println(scan.toString());
- }
- }
The above code returs the String representation of the Scanner
35. Scanner.useDelimiter()
Syntax
1. Scanner useDelimiter(Pattern pattern)
It is used to set the delimiting pattern of the Scanner which is in use.
2. Scanner useDelimiter(String pattern)
It is used to set the delimiting pattern of the Scanner which is in use.
- import java.util.Scanner;
- import java.util.regex.Pattern;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner").useDelimiter(Pattern.compile("\\s*\\s"));
- out.println(scan.delimiter());
- Scanner scan1 = new Scanner("CSharpCorner").useDelimiter("\n");
- out.println(scan1.delimiter());
- }
- }
36. Scanner.useLocale()
Syntax
Scanner useLocale(Locale locale)
The function is used to change the current locale of the Scanner to the specified locale
- import java.util.Locale;
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner").useLocale(Locale.ITALY);
- out.println(scan.locale());
- }
- }
In the above code, we are changing the locale to Italy locale
37. Scanner.useRadix()
Syntax
Scanner useRadix(int radix)
The function is used to change the current radix of the Scanner to the specified radix
- import java.util.Locale;
- import java.util.Scanner;
- import static java.lang.System.out;
- class Csharpcorner
- {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner("CSharpCorner").useRadix(6);
- out.println(scan.radix());
- }
- }
In the above code, we are changing the radix from 10 to 6
No comments:
Post a Comment