Rajinder Menu

How to read input from keyboard in Java.


This example shows how to read an input from keyboard in Java. First we are reading a String then an integer.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {
    public static void main(String s[]){
        String s1;
        int i;
       
       
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try{
       
        System.out.println("Enter any String:");
        s1= br.readLine();
       
        System.out.println("Entered String is:"+s1);
       
        System.out.println("Enter any Integer:");
        i= Integer.parseInt(br.readLine());
       
        System.out.println("Entered Integer is:"+i);
        }catch(IOException e){
            System.out.println(e);
           
        }
    }

}

Output:
Enter any String:
Hi How are you?
Entered String is:Hi How are you?
Enter any Integer:
10
Entered Integer is:10

I would like to know your comments and if you liked the article then please share it on social networking buttons.


No comments:

Post a Comment