Program to explain use of Scanner class in Java

Program in Java to input roll no and name of a student using Scanner class and print the details)

Program Code:

package javaapplication1;

import java.util.Scanner;

public class JavaApplication1 {

    public static void main(String[] args){

    Scanner in= new Scanner(System.in);

    System.out.println("Enter your Roll No"); 
    int rn= in.nextInt();

    System.out.println("Enter your name"); 

    in.nextLine();    
    /* 
       To clear the input buffer 
       As it stores newline character pressed after roll no
       Otherwise it will store that newline character into name
       And will not allow you to enter your name
       For testing, just remove this line (in.nextLine();) and run program
    */
    String name = in.nextLine();

    System.out.println("Your Roll No is " + rn);
    System.out.println("Your Name is " + name);    
    }
}

Output:

Enter your Roll No
1
Enter your name
Sheetal Garg
Your Roll No is 1
Your Name is Sheetal Garg
error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.