Thursday, April 10, 2014

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.







1 comment:

Alia parker said...

Mesmerized article composed of this web journal with other significant data. It is straight to the point that how we will make strides our aptitudes as well as how able to be spoken to a modern stream of polished skill. Cheap lidar scanner Denver, Colorado