Java Program To Read Data from Keyboard

Write Java Program to read data from the keyboard

// WAP to use scanner package for reading data

import java.util.Scanner;
public class ScannerClass 
{
    public static void main(String []args)
    {
        int a;
        float b;
        char c;
        // object 'sc' created to scan data
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter decimal number :--> ");
        a = sc.nextInt();    // read from keyboard

        System.out.println("Enter floating point number :--> ");
        b  = sc.nextFloat();    // read from keyboard

        System.out.println("Enter character  :--> ");
        c = sc.next().charAt(0);    // read from keyboard
        // charAt: return 0 index character

        //displaying values stored in variables
        System.out.println("Entered decimal number :--> "+a);
        System.out.println("Entered float number :--> "+b);
        System.out.println("Entered character  :--> "+c);
    }
}

Output:


<< Previous | Next >>


Leave a Reply

Your email address will not be published.