Skip to main content

Posts

Java Programming Tutorials: How to Read / Write data from / to Console?

 You can use "in" object of System class to read data from console. And, you can use println() method of "out" object of System class to write data to console. Source code package gettingstartedwithjava ; import java.util.Scanner ; public class ReadAndWriteToConsole { public static void main (String[] args) { // create a scanner object and pass System.in object as constructor argument Scanner reader = new Scanner(System. in ) ; // while reader has data, keep the loop running while (reader.hasNext()) { // read data to word String word = reader.nextLine() ; // print the word System. out .println(word) ; } } } This program is also known as echo-program, which means that whatever you will type in console, the same will be printed on the console. Github link:  https://github.com/rakeshnarang5/GettingStartedWithJavaProgramming/blob/main/ReadAndWriteToConsole.java
Recent posts