INPUTTING 2 CHARACTERS IN JAVA

import java.io.*;

class Check

{

public static void main(String args[])throws IOException

{

char a,b;

InputStreamReader isr=new InputStreamReader(System.in);

BufferedReader buf=new BufferedReader(isr);

a=(char)buf.read();

b=(char)buf.read();

System.out.println(a+" "+b);

}

}

I am using this code to input 2 characters in JAVA. However, on executing, only the first character is getting printed. Why is so happening? And what should I do to overcome it?

buf.read() is reading a single character, try to convert into string then u can access any part of the string.

You can use instead of BufferedReader

import java.io.IOException;

import java.util.Scanner;

public class Test {
public static void main(String[] args) throws IOException {

char a,b;
Scanner sc=new Scanner(System.in);
a=sc.next(".").charAt(0);
b=sc.next(".").charAt(0);
System.out.println(a+" "+b);

}

}