How to take Character Input in Java

In Java, the Scanner class is used to take character input from the user. The Scanner class is not a single solution of taking character input from the user. The Buffered Reader class an alternative to the Scanner class for taking character input from the user. Let’s start understanding.

These are the steps to take character input using Buffered Reader:

  1. In the first step, we have to create an instance of the Buffered Reader class by using the InputStreamReader The InputStreamReader is a class that act as a bridge in between the byte stream to character stream. The reason of using InputStreamReaderis to decode bytes to characters.
  2. We will use the read() method of the Buffered Reader class to read character from the given Buffered Reader and typecast it to char.
  3. We will store the return value of the read() method into the character type variable. The read() method returns the character read by this method. The read() method gives us result -1 in case when the buffered stream has ended and there is no character to be read.
  4. At the end, we will print the character data on the screen which we read using the read() method. Let’s take some example to understand how the Buffered Reader class is used to read character data in Java. We will take two examples, i.e., BufferedReaderExample and BufferedReaderExample2. In the first example, we will simply read the character data using the Buffered Reader class and print it to the screen. In the second example, we will get the string from user using Buffered Reader class and perform some operations on the user entered string.

BufferedReaderExample.java

    //Import classes and packages  
    import java.io.BufferedReader;  
    import java.io.IOException;  
    import java.io.InputStreamReader;  
      
    //Creating BufferedReaderExample class  
    public class BufferedReaderExample {  
        //main() method with IOException  
        public static void main(String[] args) throws IOException{  
            //Creating instance of the BufferedReader class with InputStreamReader  
            BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));  
            //Reading character data  
            char data = (char)obj.read();  
            //Printing the data on the screen  
            System.out.println(data);  
      
        }  
    }  

Output:

Output

Buffered Reader class can also be used to get the string from the user. For this, we will use the readLine() of the Buffered Reader class. Let’s understand it by taking another example of Buffered Reader. We will calculate the area of the triangle by taking its base value and height value from the user as a string using Buffered Reader class. We then convert the string data into a double by parsing string data, and then we calculate the area of the triangle by using the formula (base*height)/2.

BufferedReaderExample2.java

    //Import classes and packages  
    import java.io.BufferedReader;  
    import java.io.IOException;  
    import java.io.InputStreamReader;  
      
    //Creating BufferedReaderExample2 class  
    public class BufferedReaderExample2 {  
        //main() method with IOException  
        public static void main(String[] args) throws IOException{  
            //Creating instance of the BufferedReader class with InputStreamReader  
            BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));  
              
            System.out.print ( "Enter the height of triangle:" );  
            //Getting height of triangle  
            String hightData = obj.readLine();  
            double height = Double.parseDouble ( hightData );  
              
            System.out.print ( "Enter the base of triangle:" );  
            //Getting base of triangle  
            String baseData = obj.readLine();  
            double base = Double.parseDouble ( baseData );  
              
            //Calculating area of triangle  
            double area = (height*base)/2;  
              
            //Printing the area of the triangle  
            System.out.println("The area of triangle is:"+area );  
      
        }  
    }  

Output:

Output

www.000webhost.com