Saturday, September 14, 2019

A Complete Java Scanner Tutorial

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 

  1. Java Scanner class breaks the input into tokens using a delimiter which is whitespace by default. 
  2. It is the easiest way to read input in Java program, 
  3. 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.
  4. The Java Scanner class extends Object class and implements Iterator and Closeable interfaces.

Shortcomings of Java Scanner 

  1. 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. 
  2. Java Scanner is not Synchronized.
  3. Java Scanner is not Thread Safe
  4. Java Scanner has smaller buffer memory i.e. 1024 char
  5. 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.
  1. import java.util.Scanner;  
  2. import static java.lang.System.out;  
  3.   
  4. /** 
  5. * An example program to read a String from console input in Java 
  6. */  
  7. public class CSharpCorner  
  8. {  
  9.    public static void main(String[] args)  
  10.   {  
  11.        out.print("Enter a string : ");  
  12.        Scanner scanner = new Scanner(System. in);  
  13.        String inputString = scanner. nextLine();  
  14.        out.println("String read from console is : \n"+inputString);  
  15.    }  
  16. }  
'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' .

    Java Scanner Constructors

    1.  Scanner(File Source)

      It constructs a new Scanner that produces values scanned from the specified file   
      1. import java.io.File;    
      2. import java.io.FileNotFoundException;    
      3. import java.util.Scanner;
      4. import static java.lang.System.out;    
      5.       
      6. class Csharpcorner      
      7. {      
      8.     public static void main(String[] args) throws FileNotFoundException      
      9.     {      
      10.         Scanner sc = new Scanner(new File("example.txt"));    
      11.             
      12.         while(sc.hasNext())    
      13.         {    
      14.             out.print(sc.next());    
      15.         }    
      16.     }      
      17. }   
    2. Scanner( File Source, String charsetName)

      It constructs a new Scanner that produces values scanned from the specified file  using the given decoding technique 
      1. import java.io.File;    
      2. import java.io.FileNotFoundException;    
      3. import java.nio.charset.StandardCharsets;    
      4. import java.util.Scanner; 
      5. import static java.lang.System.out;    
      6.       
      7. class Csharpcorner      
      8. {      
      9.     public static void main(String[] args) throws FileNotFoundException      
      10.     {      
      11.         Scanner sc = new Scanner(new File("example.txt", StandardCharsets.US_ASCII.name()));    
      12.             
      13.         while(sc.hasNext())    
      14.         {    
      15.             out.print(sc.next());    
      16.         }    
      17.     }      
      18. }     
    3. Scanner(InputStream source)

      It constructs a new Scanner that produces values scanned from the specified input Stream. 
      1. import java.io.File;    
      2. import static java.lang.System.out;    
      3. import java.util.Scanner;    
      4.       
      5. class Csharpcorner      
      6. {      
      7.     public static void main(String[] args)     
      8.     {      
      9.       Scanner myObj = new Scanner (System.in);    
      10.       out.println("Enter");    
      11.           
      12.       String user = myObj.nextLine();    
      13.       out.print(user);    
      14.     }      
      15. }     
    4. Scanner(InputStream source, String charsetName)

      It constructs a new Scanner that produces values scanned from the specified input stream using the given decoding technique.
      1. import java.nio.charset.StandardCharsets;    
      2. import java.io.File;    
      3. import java.util.Scanner;   
      4. import static java.lang.System.out;    
      5.       
      6. class Csharpcorner      
      7. {      
      8.     public static void main(String[] args)     
      9.     {      
      10.       Scanner myObj = new Scanner (System.in, StandardCharsets.US_ASCII.name());    
      11.       out.println("Enter");    
      12.           
      13.       String user = myObj.nextLine();    
      14.       out.print(user);    
      15.     }      
      16. }      
    5. Scanner( Readable source)

      It constructs a new Scanner that produces values scanned from the specified source.
      1. import java.io.File;    
      2. import java.io.FileNotFoundException;    
      3. import java.io.FileReader;    
      4. import java.util.Scanner;   
      5. import static java.lang.System.out;    
      6.       
      7. class Csharpcorner      
      8. {      
      9.     public static void main(String[] args) throws FileNotFoundException      
      10.     {      
      11.         FileReader reader = new FileReader(new File("example.txt"));    
      12.         Scanner sc = new Scanner(reader);    
      13.             
      14.         while(sc.hasNext())    
      15.         {    
      16.             out.print(sc.next());    
      17.         }    
      18.     }      
      19. }      
    6. Scanner(String source)

      It constructs a new Scanner that produces values scanned from the specified string.
      1. import java.util.Scanner;  
      2. import static java.lang.System.out;    
      3.     
      4. class Csharpcorner        
      5. {        
      6.     public static void main(String[] args)       
      7.     {        
      8.         String reader = " CSharpCorner";      
      9.         Scanner sc = new Scanner(reader);      
      10.               
      11.         while(sc.hasNext())      
      12.         {      
      13.             out.print(sc.next());      
      14.         }      
      15.     }        
      16. }   
    7. Scanner(ReadableByteChannel source)

      It constructs a new Scanner that produces value scanned from the specified channel.
      1. import java.io.FileInputStream;      
      2. import java.io.FileNotFoundException;      
      3. import java.nio.charset.StandardCharsets;      
      4. import java.util.Scanner;    
      5. import static java.lang.System.out;    
      6.         
      7. class Csharpcorner        
      8. {        
      9.     public static void main(String[] args) throws FileNotFoundException        
      10.     {        
      11.         Scanner scanner = new Scanner(new FileInputStream("example.txt").getChannel());      
      12.         out.println(scanner.nextLine());    
      13.            
      14.     }        
      15. }     
    8. Scanner(ReadableByteChannel source, String charsetName)

      It constructs a new Scanner that produces value scanned from the specified channel.
      1. import java.io.FileInputStream;    
      2. import java.io.FileNotFoundException;    
      3. import java.nio.charset.StandardCharsets;    
      4. import java.util.Scanner;   
      5. import static java.lang.System.out;    
      6.       
      7. class Csharpcorner      
      8. {      
      9.     public static void main(String[] args) throws FileNotFoundException      
      10.     {      
      11.         Scanner scanner = new Scanner(new FileInputStream("example.txt").getChannel(),StandardCharsets.US_ASCII.name() );    
      12.         out.println(scanner.nextLine());  
      13.          
      14.     }      
      15. }       
    9. Scanner(Path source)

       It constructs a new Scanner that produces values scanned from the specified file.
      1. import java.io.IOException;  
      2. import java.nio.file.FileSystems;  
      3. import java.util.Scanner; 
      4. import static java.lang.System.out;    
      5.       
      6. class Csharpcorner      
      7. {      
      8.     public static void main(String[] args) throws IOException  
      9.     {      
      10.         try (Scanner scanner = new Scanner(FileSystems.getDefault().getPath("lib","jdk.lib")))   
      11.         {  
      12.             out.println(scanner.nextLine());  
      13.         }  
      14.     }      
      15. }    
    10. Scanner(Path Source, String charsetName)

      It constructs a new Scanner that produces values scanned from the specified file.
      1. import java.io.IOException;  
      2. import java.nio.charset.StandardCharsets;  
      3. import java.nio.file.FileSystems;  
      4. import java.util.Scanner; 
      5. import static java.lang.System.out;    
      6.       
      7. class Csharpcorner      
      8. {      
      9.     public static void main(String[] args) throws IOException  
      10.     {      
      11.         try (Scanner scanner = new Scanner(FileSystems.getDefault().getPath("lib","jdk.lib"),StandardCharsets.US_ASCII.name()))   
      12.         {  
      13.             out.println(scanner.nextLine());  
      14.         }  
      15.     }      
      16. }    

    Java Scanner Class Methods

     

    1. Scanner.close()

     
    Syntax
     
    void close()
     
    It is used to close the scanner
    1. import java.util.Scanner; 
    2. import static java.lang.System.out;    
    3.       
    4. class Csharpcorner      
    5. {      
    6.     public static void main(String[] args)  
    7.     {      
    8.         Scanner scan =  new Scanner("CsharpCorner");  
    9.           
    10.         scan.close();  
    11.     }      
    12. }     
    In the above code, we are demonstrating the use of close() method.

    2. Scanner.delimiter()

     
    Syntax
     
    pattern delimiter()
     
    It is used to get the Pattern which the Scanner class is currently using to match delimiters.
    1. import java.util.Scanner; 
    2. import static java.lang.System.out;    
    3.       
    4. class Csharpcorner      
    5. {      
    6.     public static void main(String[] args)  
    7.     {      
    8.         Scanner scan =  new Scanner("CsharpCorner");  
    9.         out.println(""+scan.nextLine());  
    10.         out.println(""+scan.delimiter());   
    11.         scan.close();  
    12.     }      
    13. }      
    In the above code, we will be printing the delimiter used by the Scanner object i.e. \p{javaWhitespace}+

    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
    1. import java.util.Scanner;  
    2. import java.util.regex.Pattern; 
    3. import static java.lang.System.out;    
    4.       
    5. class Csharpcorner      
    6. {      
    7.     public static void main(String[] args)  
    8.     {      
    9.         Scanner scan =  new Scanner("CsharpCorner");  
    10.         out.println(scan.findInLine("sharp"));  
    11.           
    12.         out.println(scan.findInLine(Pattern.compile("sharp")));  
    13.         scan.close();  
    14.     }      
    15. }    
    In the above code, in the first version we are finding whether we are able to find the given substring from the String. In the second version we are finding whether the given string has a pattern of the "sharp".
     

    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.
    1. import java.util.Scanner; 
    2. import static java.lang.System.out;    
    3. import java.util.regex.Pattern;  
    4.       
    5. class Csharpcorner      
    6. {      
    7.     public static void main(String[] args)  
    8.     {      
    9.         Scanner scan =  new Scanner("1111, 2222, 33333, 45556");  
    10.         Pattern pattern =  Pattern.compile("[0-9]+");  
    11.         while(scan.hasNext())  
    12.         {  
    13.             out.println(scan.findWithinHorizon(pattern, 18));  
    14.         }  
    15.           
    16.         while(scan.hasNext())  
    17.         {  
    18.             out.println(scan.findWithinHorizon("[0-9]+"10));  
    19.         }  
    20.         scan.close();  
    21.     }      
    22. }       
    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.
    1. import java.util.Scanner;  
    2. import java.util.regex.Pattern; 
    3. import static java.lang.System.out;    
    4.       
    5. class Csharpcorner      
    6. {      
    7.     public static void main(String[] args)  
    8.     {      
    9.         Scanner scan =  new Scanner("CSharpCorner");  
    10.         while(scan.hasNext())  
    11.         {  
    12.             out.print(scan);  
    13.         }  
    14.         String pattern = "Sharp";  
    15.         if(scan.hasNext(pattern))  
    16.         {  
    17.             out.println("Pattern Found");  
    18.         }  
    19.         Pattern pattern1 =  Pattern.compile("[[A-Za-z]*");  
    20.         while(scan.hasNext(pattern1))  
    21.         {  
    22.            out.println(scan.next());  
    23.         }  
    24.         scan.close();  
    25.     }      
    26. }          
    The above code, demonstrates all 3 versions of hasNext() methods.
     

    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.
    1. import java.util.Scanner; 
    2. import static java.lang.System.out;    
    3.       
    4. class Csharpcorner      
    5. {      
    6.     public static void main(String[] args)  
    7.     {      
    8.         Scanner scan =  new Scanner("CSharpCorner");  
    9.         if(scan.hasNextBigDecimal())  
    10.         {  
    11.             out.println("true");  
    12.         }  
    13.         else  
    14.         {  
    15.             out.println("false");  
    16.         }  
    17.         scan.close();  
    18.     }      
    19. }   
    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.
    1. import java.util.Scanner; 
    2. import static java.lang.System.out;    
    3.       
    4. class Csharpcorner      
    5. {      
    6.     public static void main(String[] args)  
    7.     {      
    8.         Scanner scan =  new Scanner("CSharpCorner");  
    9.         if(scan.hasNextBigInteger())  
    10.         {  
    11.             out.println(scan.nextBigInteger());  
    12.         }  
    13.         if(scan.hasNextBigInteger(6))  
    14.         {  
    15.             out.println(scan.nextBigInteger());  
    16.         }  
    17.         scan.close();  
    18.     }      
    19. }   
    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.
    1. import java.util.Scanner; 
    2. import static java.lang.System.out;    
    3.       
    4. class Csharpcorner      
    5. {      
    6.     public static void main(String[] args)  
    7.     {      
    8.         Scanner scan =  new Scanner("CSharpCorner");  
    9.         if(scan.hasNextBoolean())  
    10.         {  
    11.             out.println(scan.nextBigInteger());  
    12.         }  
    13.         scan.close();  
    14.     }      
    15. }       
     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.
    1. import java.util.Scanner;   
    2. import static java.lang.System.out;    
    3.         
    4. class Csharpcorner        
    5. {        
    6.     public static void main(String[] args)    
    7.     {        
    8.         Scanner scan =  new Scanner("CSharpCorner");    
    9.         if(scan.hasNextByte())    
    10.         {    
    11.             out.println(scan.nextByte());    
    12.         }    
    13.         if(scan.hasNextByte(6))    
    14.         {    
    15.             out.println(scan.nextByte());    
    16.         }    
    17.         scan.close();    
    18.     }        
    19. }      
    In the above code, we are demonstarting both the version of the methods with default radix and with user defined radix
     

    10. Scanner.hasNextDouble()

     
    Syntax
     
    boolean hasNextDouble()
     
    It returns true if and only if this scanner's next token is a valid double value
    1. import java.util.Scanner;   
    2. import static java.lang.System.out;    
    3.         
    4. class Csharpcorner        
    5. {        
    6.     public static void main(String[] args)    
    7.     {        
    8.         Scanner scan =  new Scanner("CSharpCorner");    
    9.         if(scan.hasNextDouble())    
    10.         {    
    11.             out.println(scan.nextDouble());    
    12.         }    
    13.         scan.close();    
    14.     }        
    15. }   
    In the above code, we are checking if the scanner input has double values
     

    11. Scanner.hasNextFloat()

     
    Syntax
     
    boolean hasNextFloat()
     
    It returns true if and only if this scanner's next token is a valid float value
    1. import java.util.Scanner; 
    2. import static java.lang.System.out;    
    3.       
    4. class Csharpcorner      
    5. {      
    6.     public static void main(String[] args)  
    7.     {      
    8.         Scanner scan =  new Scanner("CSharpCorner");  
    9.         if(scan.hasNextFloat())  
    10.         {  
    11.             out.println(scan.nextFloat());  
    12.         }  
    13.         scan.close();  
    14.     }      
    15. }   
    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.
    1. import java.util.Scanner;  
    2. import static java.lang.System.out;    
    3.           
    4. class Csharpcorner          
    5. {          
    6.     public static void main(String[] args)      
    7.     {          
    8.         Scanner scan =  new Scanner("CSharpCorner");      
    9.         if(scan.hasNextInt())      
    10.         {      
    11.             out.println(scan.nextInt());      
    12.         }      
    13.         if(scan.hasNextInt(6))      
    14.         {      
    15.             out.println(scan.nextInt());      
    16.         }      
    17.         scan.close();      
    18.     }          
    19. }         
    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
    1. import java.util.Scanner;    
    2. import static java.lang.System.out;    
    3.           
    4. class Csharpcorner          
    5. {          
    6.     public static void main(String[] args)      
    7.     {          
    8.         Scanner scan =  new Scanner("CSharpCorner");      
    9.         out.println(scan.hasNextLine());  
    10.         scan.close();      
    11.     }          
    12. }     
    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.
    1. import java.util.Scanner;  
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");        
    9.         if(scan.hasNextLong())        
    10.         {        
    11.             out.println(scan.nextLong());        
    12.         }        
    13.         if(scan.hasNextLong(6))        
    14.         {        
    15.             out.println(scan.nextLong());        
    16.         }        
    17.         scan.close();        
    18.     }            
    19. }    
    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.
    1. import java.util.Scanner;  
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");        
    9.         if(scan.hasNextShort())        
    10.         {        
    11.             out.println(scan.nextShort());        
    12.         }        
    13.         if(scan.hasNextShort(6))        
    14.         {        
    15.             out.println(scan.nextShort());        
    16.         }        
    17.         scan.close();        
    18.     }            
    19. }          
    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
    1. import java.util.Scanner;     
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");        
    9.         out.println(scan.ioException());   
    10.     }            
    11. }   
    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
    1. import java.util.Scanner;       
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");        
    9.         out.println(scan.locale());   
    10.     }            
    11. }   
    In the above code, the output we will be getting will be  "en_IN" or your system's langauge
     

    18. Scanner.match()

     
    Syntax 
     
    MatchResult match()
     
    It returns a match result for the last match operation
    1. import java.util.Scanner;       
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");     
    9.         out.print(scan.hasNext("Csharp"));  
    10.         out.println(scan.match());   
    11.     }            
    12. }    
    In the above code, we return the last match, here match is not done.
     

    19. Scanner.next()

     
    Syntax
     
    It returns the next token if it matches the specified pattern.
    1. import java.util.Scanner;        
    2. import java.util.regex.Pattern; 
    3. import static java.lang.System.out;    
    4.             
    5. class Csharpcorner            
    6. {            
    7.     public static void main(String[] args)        
    8.     {            
    9.         Scanner scan =  new Scanner("CSharpCorner");     
    10.         out.println(scan.next());  
    11.         String pattern = "[0-9]";  
    12.         out.println(scan.next(pattern));  
    13.         out.println(scan.next(Pattern.compile("[0-9]")));  
    14.     }            
    15. }     
    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.
    1. import java.util.Scanner; 
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");     
    9.         out.println(scan.nextBigDecimal());  
    10.     }            
    11. }       
    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
    1. import java.util.Scanner;    
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");     
    9.         out.println(scan.nextBigInteger());  
    10.         out.println(scan.nextBigInteger(6));  
    11.     }            
    12. }    
    In the above code, we will be printing the next BigInteger value
     

    22. Scanner.nextBoolean()

     
    Syntax
     
    boolean nextBoolean()
     
    It returns the boolean Scanned from the output.
    1. import java.util.Scanner;   
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");     
    9.         out.println(scan.nextBoolean());  
    10.     }            
    11. }    
    In the above code, we will be printing the next boolean value
     

    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
    1. import java.util.Scanner;    
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");     
    9.         out.println(scan.nextByte());  
    10.         out.println(scan.nextByte(6));  
    11.     }            
    12. }   
    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.
    1. import java.util.Scanner;  
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");     
    9.         out.println(scan.nextDouble());  
    10.     }            
    11. }    
    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.
    1. import java.util.Scanner;   
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");     
    9.         out.println(scan.nextFloat());  
    10.     }            
    11. }    
    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
    1. import java.util.Scanner;    
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");     
    9.         out.println(scan.nextInt());  
    10.         out.println(scan.nextInt(6));  
    11.     }            
    12. }   
     In the above code, we are printing the next Int value
     

    27. Scanner.nextLine()

     
    Syntax
     
    String nextLine()
     
    It returns the String Scanned from the output.
    1. import java.util.Scanner;  
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");     
    9.         out.println(scan.nextLine());  
    10.     }  
    11. }   
    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
    1. import java.util.Scanner;       
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");     
    9.         out.println(scan.nextLong());  
    10.         out.println(scan.nextLong(6));  
    11.     }            
    12. }    
     In the above code, we are printing the next Long value.
     

    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
    1. import java.util.Scanner;    
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");     
    9.         out.println(scan.nextShort());  
    10.         out.println(scan.nextShort(6));  
    11.     }            
    12. }   
    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.
    1. import java.util.Scanner; 
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");     
    9.         out.println(scan.radix());  
    10.    }            
    11. }     
    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
    1. import java.util.Scanner;    
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");     
    9.         out.println(scan.next());  
    10.         scan.remove();  
    11.         out.println(scan.next());  
    12.    }            
    13. }   
    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.
    1. import java.util.Scanner;      
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");     
    9.         out.println(scan.next());  
    10.         out.println(scan.reset());  
    11.    }            
    12. }   
    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
    1. import java.util.Scanner;        
    2. import java.util.regex.Pattern; 
    3. import static java.lang.System.out;    
    4.             
    5. class Csharpcorner            
    6. {            
    7.     public static void main(String[] args)        
    8.     {            
    9.         Scanner scan =  new Scanner("CSharpCorner");     
    10.         out.println(scan.next());  
    11.         String pattern = "[A-Z]*";  
    12.         while(scan.hasNext())  
    13.         {  
    14.             scan.skip(pattern);  
    15.             out.println(scan.next());  
    16.         }  
    17.         Scanner scan1 =  new Scanner("CSharpCorner");    
    18.         scan1.skip(Pattern.compile("...ar"));  
    19.         out.println(scan1.nextLine());  
    20.    }            
    21. }           
    In the above code, we are demonstrating the use of the method
     

    34. Scanner.toString()

     
    Syntax
     
    String toString()
     
    It returns the String representation of this Scanner
    1. import java.util.Scanner;  
    2. import static java.lang.System.out;    
    3.             
    4. class Csharpcorner            
    5. {            
    6.     public static void main(String[] args)        
    7.     {            
    8.         Scanner scan =  new Scanner("CSharpCorner");     
    9.         out.println(scan.toString());  
    10.    }            
    11. }     
    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.
    1. import java.util.Scanner;        
    2. import java.util.regex.Pattern; 
    3. import static java.lang.System.out;    
    4.             
    5. class Csharpcorner            
    6. {            
    7.     public static void main(String[] args)        
    8.     {            
    9.         Scanner scan =  new Scanner("CSharpCorner").useDelimiter(Pattern.compile("\\s*\\s"));     
    10.         out.println(scan.delimiter());  
    11.           
    12.         Scanner scan1 =  new Scanner("CSharpCorner").useDelimiter("\n");     
    13.         out.println(scan1.delimiter());  
    14.    }            
    15. }  
    In the above code, we first gave a patterns as the delimiter and then a string 
     

    36. Scanner.useLocale()

     
    Syntax
     
    Scanner useLocale(Locale locale)
     
    The function is used to change the current locale of the Scanner to the specified locale
    1. import java.util.Locale;  
    2. import java.util.Scanner;    
    3. import static java.lang.System.out;    
    4.             
    5. class Csharpcorner            
    6. {            
    7.     public static void main(String[] args)        
    8.     {            
    9.         Scanner scan =  new Scanner("CSharpCorner").useLocale(Locale.ITALY);     
    10.         out.println(scan.locale());  
    11.          
    12.    }            
    13. }   
     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
    1. import java.util.Locale;  
    2. import java.util.Scanner;  
    3. import static java.lang.System.out;    
    4.             
    5. class Csharpcorner            
    6. {            
    7.     public static void main(String[] args)        
    8.     {            
    9.         Scanner scan =  new Scanner("CSharpCorner").useRadix(6);     
    10.         out.println(scan.radix());  
    11.          
    12.    }            
    13. }   
    In the above code, we are changing the radix from 10 to 6

    No comments:

    Post a Comment

    How to DROP SEQUENCE in Oracle?

      Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...