Thursday, April 24, 2014

Scanner class - III [Examples]


Token parsing using default delimiter and user defined delimiter-



import java.util.*;
    public class ScanDL
        {
            public static void main(String args[])
                {
                    String St;
                    Scanner sc1;
                    Scanner sc=new Scanner(System.in);
                    System.out.print("\tEnter a Sentence : ");
                    St=sc.nextLine();
                    sc1=new Scanner(St);//.useDelimiter(" ");
                    System.out.println("\n\tDefaul Delimiter :\n");                   
                    while(sc1.hasNext())
                        {
                            System.out.println("\t"+sc1.next());
                        }
                    sc1.close();
                    System.out.print("\tEnter a Sentence : ");
                    St=sc.nextLine();
                    sc1=new Scanner(St).useDelimiter(",");   
                    System.out.println("\n\tUser Defined Delimiter :\n");                    
                    while(sc1.hasNext())
                        {
                            System.out.println("\t"+sc1.next().trim());
                        }                   
               }
       }


Output:



                                  Enter a Sentence : Hope is a waking dream.

          Defaul Delimiter :

          Hope
          is
          a
          waking
          dream.
                             Enter a Sentence : Hard Work, Honesty, Dedication, all matters.

          User Defined Delimiter :

          Hard Work
          Honesty
          Dedication
          all matters.




Token parsing using multiple delimiter.



import java.util.*;
    public class ScanDelim
        {
            public static void main(String args[])
                {
                    String St;
                    int a;
                    Scanner sc=new Scanner(System.in);
                    System.out.print("\tEnter a Sentence : ");
                    St=sc.nextLine();
                    Scanner sc1=new Scanner(St).useDelimiter("\\.\\s|\\?\\s|\\!\\s");
                    while(sc1.hasNext())
                        {
                            System.out.println("\t"+sc1.next());
                        }
               }
        }


Output



          Enter a Sentence : Hello! How are you? Hope, all is well. I am fine too.

          Hello
          How are you
          Hope, all is well
          I am fine too.

Thursday, April 10, 2014

Scanner Class - II


Some other useful Methods of the  Scanner Class :

Method Name
Defination
close()
Closes the scanner.
delimiter()
Returns the Pattern used by current Scanner object.
findInLine(Pattern pattern)
This method returns a String object that satisfies the Pattern.
findInLine(String pattern)
find the next occurrence of the specified pattern
findWithinHorizon(Pattern pattern, int horizon)
Find the next occurrence of the specified pattern.
findWithinHorizon(String pattern, int horizon)
Find the next occurrence of a pattern input ignoring delimiter
hasNext()
Returns true if this scanner has another token.
hasNext(Pattern pattern)
Returns true if the next token matches the specified pattern.
hasNext(String pattern)
Returns true if the next token matches the pattern constructed from the specified string.
hasNextBigDecimal()
Returns true if the next token in this scanner's input can be interpreted as a BigDecimal using the nextBigDecimal() method.
hasNextBigInteger()
Returns true if the next token in this scanner's input can be interpreted as a BigInteger in the default radix using the nextBigInteger() method.
hasNextBigInteger(int radix)
Returns true if the next token in this scanner's input can be interpreted as a BigInteger in the specified radix using the nextBigInteger() method.
hasNextBoolean()
This method checks if the Scanner object has boolean data type on its buffer.
hasNextByte()
This method returns true if the next byte on the scanner buffer can be translated to byte data type otherwise false.
hasNextByte(int radix)
Returns true if the next token in this scanner's input can be interpreted as a byte value in the specified.
hasNextDouble()
Returns true if the next token in this scanner's input can be interpreted as a double value using the nextDouble() method.
hasNextFloat()
Returns true if the next token in this scanner's input can be interpreted as a float value using the nextFloat() method.
hasNextInt()
Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method.
hasNextInt(int radix)
This method returns boolean, true if the token can be interpreted as int data type with respect to the radix used by the scanner object otherwise false.
hasNextLine()
This method returns a boolean data type which corresponds to the existence of new line on the String tokens which the Scanner object holds.
hasNextLong()
Returns true if the next token in this scanner's input can be interpreted as a long value in the default radix using the nextLong() method.
hasNextLong(int radix)
Returns true if the next token in this scanner's input can be interpreted as a long value in the specified radix using the nextLong() method.
hasNextShort()
Returns true if the next token in this scanner's input can be interpreted as a short value in the default radix using the nextShort() method.
hasNextShort(int radix)
This method returns boolean, true if the token can be interpreted as short data type with respect to the radix used by the scanner object otherwise false.
ioException()
Returns the IOException last thrown by this Scanner's underlying Readable.
locale()
This method returns a Locale which the Scanner class is using.
match()
This method returns a MatchResult object which corresponds to the result of the last operation by the scanner object.
next(Pattern pattern)
Returns the next token if it matches the specified pattern.
radix()
Returns this scanner's default radix.
remove()
The remove operation is not supported by this implementation of Iterator.
reset()
Resets this scanner.
skip(Pattern pattern)
Skips input that matches the specified pattern, ignoring delimiters.
skip(String pattern)
Skips input that matches a pattern constructed from the specified string.
toString()
Returns the string representation of this Scanner.
useDelimiter(Pattern pattern)
Sets this scanner's delimiting pattern to the specified pattern.
useDelimiter(String pattern)
Sets this scanner's delimiting pattern to a pattern constructed from the specified String.
useLocale(Locale locale)
Sets this scanner's locale to the specified locale.
useRadix(int radix)
Sets this scanner's default radix to the specified radix.

Scnner Class - I


The Scanner class is a class in java.util. Scanner class allows user to get input from keyboard.  Scanner class also having many other methods that helps to read a text files and also breaks the input into tokens using a delimiter, is whitespace considered as default delimiter.

Some methods of Scanner class for reading data.



String next() -               Read the input only till the space
String nextLine() -         Reads till the end of line ( till ‘\n’)
Int nextInt() -                Read a Integer data from System.in
byte nextByte() -          Read tokens in byte data type.
boolean nextBoolean() - Returns boolean data type
double nextDouble() -    Read next token of the input as a double.
float nextFloat() -          Read next token of the input as a Float.
long nextLong() -           Read next token of the input as a :ong.


Example:


import java.util.*;
import java.io.*;
    class Scanner_Read
        {
            public static void main(String args[])throws IOException
                {
                    Scanner Sc=new Scanner(System.in);
                    System.out.print("\n\tEnter a Word : ");
                    String s=Sc.next();
                    String s1=Sc.nextLine(); // Clearing Buffer
                    System.out.print("\n\tEnter a Sentence : ");
                    s1=Sc.nextLine();
                    System.out.print("\n\tEnter a Byte : ");
                    byte b=Sc.nextByte();
                    System.out.print("\n\tEnter an Integer Data : ");
                    int n=Sc.nextInt();
                    s1=Sc.nextLine(); // Clearing Buffer
                    System.out.print("\n\tEnter an Double Data : ");
                    double d=Sc.nextDouble();
                    System.out.print("\n\tEnter an Float Data : ");
                    float f=Sc.nextFloat();
                    System.out.print("\n\tEnter an Long Data : ");
                    long L=Sc.nextLong();
                    System.out.println("\n\tInput Data : ");
                    System.out.println("\tWord : "+s+" Sentence : "+s1+" Byte "+b);
                    System.out.println("\tInteger : "+n+" Double : "+d+" Long "+L+" Float :"+f);

                }
        }




Output:



          Enter a Word : Hello
          Enter a Sentence : Hello! Fine.
          Enter a Byte : 12
          Enter an Integer Data : 234
          Enter an Double Data : 34.56
          Enter an Float Data : 33.4
          Enter an Long Data : 343434

          Data :
          Word : Hello Sentence :  Byte 12
          Integer : 234 Double : 34.56 Long 343434 Float :33.4


Other Scanner Class Methods for reading data


nextBigDecimal() -                       Read the next token of the input as a BigDecimal.
nextBigInteger() -                         Read the next token of the input as a BigInteger.
nextBigInteger(int radix) -             Read the next token of the input as a BigInteger.
nextByte(int radix) -                     Read the next token of the input as a byte.
nextInt(int radix) -                        Read the next token of the input as an int.
nextLong() -                                 Read the next token of the input as a long.
nextLong(int radix) -                     Read the next token of the input as a long.
nextShort() -                                Read the next token of the input as a short.
nextShort(int radix) -                    Read the next token of the input as a short.