Java program to read user input using Console

Read user input value using java.io.Console:

Java Console class, i.e. java.io.Console can be used to read user input values. In this post, we will learn how to use this class to read different user inputs.

Java program:

This program will not work from an IDE. you need to run this program from the terminal.

import java.io.Console;

public class Main {

    public static void main(String[] args) {
        Console console = System.console();

        System.out.println("Enter a string : ");
        String str = console.readLine();
        System.out.println("You have entered : " + str);

        System.out.println("Enter a character : ");
        char ch = console.readLine().toCharArray()[0];
        System.out.println("You have entered : " + ch);

        System.out.println("Enter an integer : ");
        int no = Integer.parseInt(console.readLine());
        System.out.println("You have entered : " + no);

        System.out.println("Enter a Float : ");
        float floatValue = Float.parseFloat(console.readLine());
        System.out.println("You have entered : " + floatValue);

        System.out.println("Enter a boolean : ");
        boolean bool = Boolean.parseBoolean(console.readLine());
        System.out.println("You have entered : " + bool);
    }
}

It will give output as like below:

Enter a string : 
hello world
You have entered : hello world
Enter a character : 
x
You have entered : x
Enter an integer : 
10
You have entered : 10
Enter a Float : 
22.3
You have entered : 22.3
Enter a boolean : 
true
You have entered : true

You might also like: