0

I am new to programming world and learning java these days as my first programming language. I wanted to input characters in my program. But soon I realized that once I used System.in.read() and assigned it with any char variable, it works. But after it, when I use System.in.read() and assign this statement to another char variable then I couldn't input any characters. Why is this happening and what is cure of it?

public class ReadInp {
    public static void main(String Array[]) 
    throws java.io.IOException {
    char ch1, ch2;
    System.out.println("Write first alphabet of English.");
    ch1 = (char) System.in.read();
    if(ch1=='A') {
        System.out.println("You can write one more character.");
        ch2 = (char) System.in.read();
        System.out.println("ch1 = "+ch1+"\tch2 = "+ch2);
    }
  }
}

Output :-

Write first alphabet of English.

A

You can write one more character. (As you can see in my input code, that after the execution of "You can write one more character", it must gave me opportunity to input once more so that it would assigned to ch2. But in output it didn't give me opportunity to input any type of character and ultimately ch2 remained unassigned.)

ch1 = A ch2 =

0 Answers0