The perfect place for easy learning...

Java Programming

×

Topics List


Console IO Operations in Java





Reading console input in java

In java, there are three ways to read console input. Using the 3 following ways, we can read input data from the console.

  • Using BufferedReader class
  • Using Scanner class
  • Using Console class

Let's explore the each method to read data with example.

1. Reading console input using BufferedReader class in java

Reading input data using the BufferedReader class is the traditional technique. This way of the reading method is used by wrapping the System.in (standard input stream) in an InputStreamReader which is wrapped in a BufferedReader, we can read input from the console.

The BufferedReader class has defined in the java.io package.

Consider the following example code to understand how to read console input using BufferedReader class.

Example
import java.io.*;

public class ReadingDemo {

	public static void main(String[] args) throws IOException {

		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		
		String name = "";
		
		try {
			System.out.print("Please enter your name : ");
			name = in.readLine();
			System.out.println("Hello, " + name + "!");
		}
		catch(Exception e) {
			System.out.println(e);
		}
		finally {
			in.close();
		}
	}
}

When we run the above program, it produce the following output.

BufferedReader class in java

2. Reading console input using Scanner class in java

Reading input data using the Scanner class is the most commonly used method. This way of the reading method is used by wrapping the System.in (standard input stream) which is wrapped in a Scanner, we can read input from the console.

The Scanner class has defined in the java.util package.

Consider the following example code to understand how to read console input using Scanner class.

Example
import java.util.Scanner;

public class ReadingDemo {

	public static void main(String[] args) {

		Scanner in = new Scanner(System.in);
		
		String name = "";
			System.out.print("Please enter your name : ");
			name = in.next();
			System.out.println("Hello, " + name + "!");
		
	}
}

When we run the above program, it produce the following output.

Scanner class in java

3. Reading console input using Console class in java

Reading input data using the Console class is the most commonly used method. This class was introduced in Java 1.6 version.

The Console class has defined in the java.io package.

Consider the following example code to understand how to read console input using Console class.

Example
import java.io.*;

public class ReadingDemo {

	public static void main(String[] args) {
		
		String name;
        Console con = System.console();
		
		if(con != null) {
			name = con.readLine("Please enter your name : ");
			System.out.println("Hello, " + name + "!!");
		}
		else {
			System.out.println("Console not available.");
		}
	}
}

Writing console output in java

In java, there are two methods to write console output. Using the 2 following methods, we can write output data to the console.

  • Using print() and println() methods
  • Using write() method

Let's explore the each method to write data with example.

1. Writing console output using print() and println() methods

The PrintStream is a bult-in class that provides two methods print() and println() to write console output. The print() and println() methods are the most widely used methods for console output.

Both print() and println() methods are used with System.out stream.

The print() method writes console output in the same line. This method can be used with console output only.

The println() method writes console output in a separete line (new line). This method can be used with console ans also with other output sources.

Let's look at the following code to illustrate print() and println() methods.

Example
public class WritingDemo {

	public static void main(String[] args) {

		int[] list = new int[5];
		
		for(int i = 0; i < 5; i++)
			list[i] = i*10;
		
		for(int i:list)
			System.out.print(i);	//prints in same line
		
		System.out.println("");
		for(int i:list)
			System.out.println(i);	//Prints in separate lines	
		
	}
}

When we run the above program, it produce the following output.

Scanner class in java

2. Writing console output using write() method

Alternatively, the PrintStream class provides a method write() to write console output.

The write() method take integer as argument, and writes its ASCII equalent character on to the console, it also acept escape sequences.

Let's look at the following code to illustrate write() method.

Example
public class WritingDemo {

	public static void main(String[] args) {
        
		int[] list = new int[26];
		
		for(int i = 0; i < 26; i++) {
			list[i] = i + 65;
		}
		
		for(int i:list) {
			System.out.write(i);
			System.out.write('\n');
		}		
	}
}

When we run the above program, it produce the following output.

Console class in java