0% found this document useful (0 votes)
75 views9 pages

3.8 Reading and Writing Console

Uploaded by

mugilan7778
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views9 pages

3.8 Reading and Writing Console

Uploaded by

mugilan7778
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

WAYS TO READ INPUT FROM CONSOLE

There are 3 ways for reading input from users in the command line environment.

1. Using Bufferedreader Class


This method is used by wrapping the System.in an InputStreamReader which is wrapped in a
BufferedReader.

Advantages:
The input is buffered for efficient reading.

Drawback:
The wrapping code is hard to remember.

Example:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test
{
public static void main(String[] args) throws IOException
{
//Enter data using BufferReader
BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));

// Reading data using readLine


String name = reader.readLine();

// Printing the read line


System.out.println(name);
}
}

INPUT : GEEK
OUTPUT: GEEK

2.Using Scanner Class

● This method is the most preferred method to take input.

● The main purpose of scanner class is to parse primitive types and strings using regular
expressions.

● It can be also used to read input from the user in the command line.

Advantages:
Convenient methods for parsing primitives (nextInt(), nextFloat(), …) from the tokenized input.
Regular expressions can be used to find tokens.

Drawback:
The reading methods are not synchronized.

Example

import java.util.Scanner;
class GetInputFromUser
{
public static void main(String args[])
{
// Using Scanner for Getting Input from User
Scanner in = new Scanner(System.in);
String s = in.nextLine();

System.out.println("You entered string "+s);

int a = in.nextInt();

System.out.println("You entered integer "+a);

float b = in.nextFloat();

System.out.println("You entered float "+b);

Output:

Geeksforgreeks 12 3.4
You entered String :Greeksforgreeks
You entered Integer:12
You entered float:3.4

3.Using Console Class

● Preferred way for reading user’s input from the command line.

● It can be used for reading password-like input without echoing the characters entered by the user;
● The format string syntax can also be used (like System.out.printf()).

● The java.io.Console class is attached to the system console internally.

Advantages:
● The input is buffered for efficient reading. Reading password without echoing the entered
characters.

● Reading methods are synchronized.

● Format string syntax can be used.

Drawback:
Does not work in non-interactive environments (such as in an IDE).

Java Console class declaration:

public final class Console extends Object implements Flushable

Simple example to read text from console:

String text=System.console().readLine();

System.out.println("Text is: "+text);

Java Console class methods:

Method Description
Reader reader() It is used to retrieve the reader object associated
with the console.

String readLine() It is used to read a single line of text from the


console.

String readLine(String fmt, Object... args) It provides a formatted prompt then reads the single
line of text from the console.

char[] readPassword() It is used to read password that are not being


displayed on the console.

char[] readPassword(String fmt, Object... It provides a formatted prompt then reads the
args) password that is not being displayed on the console.

Console format(String fmt, Object... args) It is used to write a formatted string to the console
output stream.

Console printf(String format, Object... args) It is used to write a string to the console output
stream.

PrintWriter writer() It is used to retrieve the PrintWriter object


associated with the console.
void flush() It is used to flushes the console.

How to get the object of Console

System class provides a static method console() that returns the singleton instance of Console class.

public static Console console()

Let's see the code to get the instance of Console class.

Console c=System.console();

Java Console

Example:

import java.io.Console;

class ReadStringTest

public static void main(String args[])

Console c=System.console();

System.out.println("Enter your name: ");


String n=c.readLine();

System.out.println("Welcome "+n);

Enter your name: Nakul Jain

Welcome Nakul Jain

Example

public class Sample

public static void main(String[] args)

// Using Console to input data from user

String name = System.console().readLine();

System.out.println(name);

}
Java Console Example to read password

import java.io.Console;

class ReadPasswordTest

public static void main(String args[])

Console c=System.console();

System.out.println("Enter password: ");

char[] ch=c.readPassword();

String pass=String.valueOf(ch);//converting char array into string

System.out.println("Password is: "+pass);

Enter password: Password is: 123

You might also like