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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 429
    Comment on it

    Scanner Class in Java

    Scanner is a class contained in java.util package basically used for obtaining the input of the primitive types like int, double,and strings. To read input an object of Scanner class is created and various methods are used to read specific input e.g to read string nextLine() is used,to read int nextInt() is used and so on.

    Program demonstrating use of Scanner class:

    1. // Java program to read data of various types using Scanner class.
    2.  
    3. import java.util.Scanner;
    4. public class ScannerDemo
    5. {
    6. public static void main(String[] args)
    7. {
    8. Scanner sc = new Scanner(System.in);
    9. String name = sc.nextLine(); // String input
    10. char gender = sc.next().charAt(0); // Character input
    11.  
    12. int age = sc.nextInt(); // Integer input
    13.  
    14. long mobileNo = sc.nextLong(); // Long input
    15.  
    16. double cgpa = sc.nextDouble(); // Double input
    17. System.out.println("Name: "+name);
    18.  
    19. System.out.println("Gender: "+gender);
    20.  
    21. System.out.println("Age: "+age);
    22.  
    23. System.out.println("Mobile Number: "+mobileNo);
    24.  
    25. System.out.println("CGPA: "+cgpa);
    26. }
    27. }

    Input :

    Suraj
    M
    20
    8976745454
    3.44444

    Output:

    1. Name: Suraj
    2. Gender: M
    3. Age: 20
    4. Mobile Number: 8976745454
    5. CGPA: 3.44444

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: