Reading and Writing at Console in Java

We will be discussing the basics of an interactive program. This lecture will be all about Input from and Output to the Console.

Input from Console

There are several ways to read data given by the user at the console. They are:

  • Scanner class
  • BufferedReader class
  • Console class

Scanner Class

This is the most common and preferred method to take input. It is used to read input from the console. However, it was initially brought up for the purpose of parsing strings and primitive data types. With this feature of parsing the tokenized input using regex and a series of methods that can be used to parse primitive data, makes it a popular choice to read input.

Example:

import java.util.Scanner; 
  
public class ScannerTest 
{ 
    public static void main(String args[]) 
    { 
        // Using Scanner for Getting Input using System.in[ Default Configuration is Console ]
        Scanner in = new Scanner(System.in); 
        System.out.println("Please enter some string: ");
        String myString = in.nextLine(); //This parses to string
        
  
        System.out.println("Please enter some integer: ");
        int myInt = in.nextInt(); //This parses to integer
  
        System.out.println("Please enter some float: ");
        float myFloat = in.nextFloat();  //This parses to float
        
        System.out.println("*******Validation Part*******");
        System.out.println("Your input string: "+myString); 
        System.out.println("Your input int: "+myInt); 
        System.out.println("Your input float: "+myFloat); 
    } 
}

Input:

Please enter some string: Madhurav
Please enter some integer: 25
Please enter some float: 25.87

Output:

Please enter some string: Madhurav
Please enter some integer: 25
Please enter some float: 25.87
*******Validation Part*******
Your input string: Madhurav
Your input int: 25
Your input float: 25.87

 BufferedReader Class

This is the classical way to take input, which was part of JDK's first release. Here, we instantiate the BufferedReader class by wrapping an InputStreamReader which wraps the standard console input stream. It buffers the input for efficient reading. It is accompanied by static methods of Wrapper classes to read corresponding data types.

import java.io.*; 
  
public class BufferedReaderTest 
{ 
    public static void main(String args[]) throws IOException 
    { 
        // Using Scanner for Getting Input using System.in[ Default Configuration is Console ]
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
        System.out.println("Please enter some string: ");
        String myString = in.readLine(); //This parses to string
        
  
        System.out.println("Please enter some integer: ");
        int myInt = Integer.parseInt(in.readLine()); //This parses to integer
  
        System.out.println("Please enter some float: ");
        float myFloat = Float.parseFloat(in.readLine());  //This parses to float
        
        System.out.println("*******Validation Part*******");
        System.out.println("Your input string: "+myString); 
        System.out.println("Your input int: "+myInt); 
        System.out.println("Your input float: "+myFloat); 
    } 
}

 Input:

Please enter some string: Madhurav
Please enter some integer: 25
Please enter some float: 25.87

Output:

Please enter some string: Madhurav
Please enter some integer: 25
Please enter some float: 25.87
*******Validation Part*******
Your input string: Madhurav
Your input int: 25
Your input float: 25.87

Console Class

It is another for reading the user’s input from the command line being synchronized. In addition, it has the feature of maintaining secrecy while reading password-like input without echoing the input. The string can also be formatted.

public class Main//ConsoleTest 
{ 
    public static void main(String args[])
    { 
        String myString = System.console().readLine("Please enter username: ");
        char[] myPassword = System.console().readPassword("Please enter password: ");
  
        System.out.println("*******Validation Part*******");
        System.out.println("Your input username: "+myString); 
        System.out.print("Your input pasword: ");
        for(char x:myPassword){
        System.out.print(x);    
        }
    } 
}

Input:

Please enter username: Madhurav
Please enter password:                                                                                                                                                                                                    

Output:

Please enter username: Madhurav                                                                                               
Please enter password:                                                                                                        
*******Validation Part*******                                                                                                 
Your input username: Madhurav                                                                                                 
Your input pasword: 12345

Output to Console

There are two ways to write at the console. Both the ways belong to class PrintStream( Character-based class ).

  • print and.println methods: Here, the methods are defined by class Printstream and is referenced by System.out. These methods are widely used.
  • write method: It is also defined by Printstream class. It is not recommended for use.

Let us look at a cumulative example.

public class Main 
{ 
    public static void main(String args[])
    { 
      System.out.println("*****I am System.out.print. I print the passed string*****");
      for(int i=1;i<=5;i++){
          System.out.print(i);
      }  
      
      System.out.println("\n*****I am System.out.println. I print the passed string and end with a new line*****");
      for(int i=1;i<=5;i++){
          System.out.println(i);
      }
      
       System.out.println("*****I am System.out.write. I accept the int byteval as input parameter *****");
      for(int x=65;x<=70;x++){
          System.out.write(x);
          System.out.write('\n');
      }
    } 
}

Output:

*****I am System.out.print. I print the passed string*****                                                                      
12345                                                                                                                           
*****I am System.out.println. I print the passed string and end with a new line*****                                            
1                                                                                                                               
2                                                                                                                               
3                                                                                                                               
4                                                                                                                               
5                                                                                                                               
*****I am System.out.write. I accept the int byteval as input parameter *****                                                   
A                                                                                                                               
B                                                                                                                               
C                                                                                                                               
D                                                                                                                               
E                                                                                                                               
F 

This was an overview of Console based Input/Output. We will be discussing Read and Write Files in the next lecture.