How to read user input from console in Java 25

problem

You’re writing a console application and wish to let the user input some data into the app like his name, age, etc. In this post we will see how to do that using Java 25.

SOLUTION

Using the IO library and the static methods, in this case the readln() method. For example, let’s read user’s name:

Main.java
				
					void main() {
   String name = IO.readln("Please enter your name: ");
   IO.println("Hello " + name);
}

				
			
running

Running it in the IDE, in this case, IntelliJ. Or in your terminal.

output
read from console java 25

conclusion

In Java 25, reading and writing to the console became so much simpler and easier in comparison with previous versions of Java, e.g. Java version 8.

Share it!

Facebook
Twitter
LinkedIn
Reddit
Picture of Ellion

Ellion

Professional IT consultant, writer, programmer enthusiast interested in all sorts of coding.
Eats all cookies 🍪