Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • java.util.Scanner Class

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 461
    Comment on it

    java.util.Scanner Class:

    It is used to read input from keyboard. It breaks the input provided into tokens using a delimeter (Whitespace). It extends Object  class and implements Iterator & Closeable interface. To use Scanner class we have to import java.util.Scanner package.

    Commonly used methods of Scanner class:

    public String next() : returns the next token from the scanner.

    public int nextInt():  scans the next token as an int value.

    public float nextFloat(): scans the next token as a float value

    public double nextDouble(): scans the next token as a double value.


    Example:

       

     import java.util.Scanner;
        class ScannerDemo{
         public static void main(String args[]){
           Scanner scan=new Scanner(System.in);
    
           System.out.println("Enter your ID");
           int id=scan.nextInt();       // reading ID provided by user
           System.out.println("Enter your name");
           String name=scan.next();      // reading name provided by user
           System.out.println("Enter your Salary");
           double salary=scan.nextDouble();       // reading salary provided by user
           System.out.println("ID:"+id+" name:"+name+" salary:"+salary);
           scan.close();
         }
        }



      Output:

         

           Enter your ID
           101
           Enter your name
           Ajay
           Enter your Salary
           450000
           ID:101 name:Ajay Salary:15000

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: