Hence System.out is an object used for standard output stream and System.in and System.err are the objects for standard input stream and error.
Reading and Writing Console I/O
• In
this section, we will understand how to read the input from console?
• In
Java, System is a class defined in java.lang and in, out and err are the
variables of the class System.
• Hence
System.out is an object used for standard output stream and System.in and
System.err are the objects for standard input stream and error.
Method
1: Reading input using Buffered Reader, Inputstream Reader, system. in
• When
we want to read some character from the console we should make use of
system.in.
• The
character stream class BufferedReader is used for this purpose. In fact we
should the variable System.in to BufferedReader object. Along with it we should
also mention the abstract class of BufferedReader class which is
InputStreamReader.
• Hence
the syntax is,
BufferedReader
object_name =new
BufferedReader(new
InputStreamReader(System.in));
For
example the object named obj can be created as
BufferedReader
obj =new BufferedReader(new InputStreamReader(System.in));
• Then,
using this object we invoke read() method. For e.g. obj.read().
• But
this is not the only thing needed for reading the input from console; we have
to mention the exception IOException for this purpose. Hence if we are going to
use read() method in our program then function main can be written like this -
public
static void main(String args[]) throws IOException
• Let us
understand all these complicated issues with the help of some simple Java
program
Java program [ReadChar.java]
/*
This is
a java program which is for reading the input from console
*/
import
java.io.*;
class
ReadChar
{
public
static void main(String args[])
throws
IOException
{
//declaring
obj for read() method
BufferedReader
obj=new BufferedReader(new InputStreamReader(System.in));
int
count=0;
char ch;
{
System.out.println("\n
Enter five characters");
while(count<5)
{
ch=(char)obj.read();//reading
single character System.out.println(ch);//outputting it
count++;//count
to keep track of 5 characters
}
}
}
Output
Enter
five characters
hello
h
e
l
l
o
Program Explanation
• This
program allows you take the input from console.
• As
given in above program, read() method is used for reading the input from the
console.
• The method read() returns the integer value,
hence it is typecast to char because we are reading characters from the
console.
• And
last but not least we should write,
import
java.io.*;
in our
java program, because these I/O operations are supported by the java package
java.io. Let us discuss one more example of reading input from console.
Ex. 4.2.1: Write a Java program
to read entire line through keyboard.
Sol. Java Program
[ReadString.java]
/*
This is
a java program which is for reading the input from console
*/
import
java.io.*;
class
ReadString
{
public
static void main(String args[])
throws
IOException
{
BufferedReader
obj=new BufferedReader(new InputStreamReader(System.in));
String
s;
s=obj.readLine();
//for reading the string
while(!s.equals("end"))
{
System.out.println(s);
s=obj.readLine();
}
}
}
Output
hello
how are you
hello
how are you
end
Note
that to read the string input we have used readLine() function. Otherwise this
program is almost same as previous one.
Ex. 4.2.2 Write a Java Program by
which user can enter his/her lucky number using keyboard. This number should
also be displayed on the console.
Sol.:
import
java.io.*;
public
class ReadNumber
{
public
static void main(String[] args) throws IOException
{
String
s;
int num;
BufferedReader
reader;//create BufferedReader object
reader=new
BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter
your Lucky number:");
s=reader.readLine();
num=Integer.parseInt(s);
//string value is converted to integer value
System.out.println("Your
Lucky number is "+num);
}
}
Output
Method 2: Reading Console input using Scanner
and System.in
• The
Scanner is a class defined in java.util package. With the help of System.in we
can read the console input by creating the instance for the class Scanner.
• The
steps to read the console input are as follows
o Step 1: Import java.util package
o Step 2: Create instance for Scanner
class as follows
Scanner scanner = new Scanner(System.in);
o Step 3: Invoke the nextXX() method to
read the input of specific data type
• Example 1: Reading String
import
java.util.*; //Step 1
public
class Example
{
public
static void main(String[] args)
{
Scanner
scanner = new Scanner(System.in); //Step
2
System.out.print("What
is your Name? ");
String
name = scanner.next(); //Step 3
System.out.println("My
Name is: "+name);
}
}
Output
What is your
Name? Ankita
My Name
is: Ankita
• Example 2: Reading integer
import
java.util.*;
public
class Example
{
public
static void main(String[] args)
{
Scanner
scanner = new Scanner(System.in);
System.out.print("Enter
your Lucky number ");
int num=
scanner.nextInt();
System.out.println("My
Lucky number is: "+num);
}
}
Output
Enter
your Lucky number 5
My Lucky
number is: 5
• Example 3: Reading float
import
java.util.*;
public
class Example
{
public
static void main(String[] args)
{
Scanner
scanner = new Scanner(System.in);
System.out.print("Enter
your marks ");
float
num= scanner.nextFloat();
System.out.println("My
marks are: "+num);
}
}
• Example 4: Reading double
import
java.util.*;
public
class Example
{
public
static void main(String[] args)
{
Scanner
scanner = new Scanner(System.in);
System.out.print("Enter
double number ");
double
num = scanner.nextDouble();
System.out.println("You
have entered: "+num);
}
}
Output
Enter
double number 212.444
You have
entered: 212.444
• Example 5: Reading boolean
import java.util.*;
public
class Example
{
public
static void main(String[] args)
{
Scanner
scanner = new Scanner(System.in);
System.out.print("Enter
boolean value as true or false ");
boolean
b = scanner.nextBoolean();
System.out.println("You
have entered: "+b);
}
}
Output
Enter
boolean value as true or false false
You have
entered: false
Ex. 4.2.3: Write a Java program
for calculating the bill amount. User enters the price of the item and its
quantity.
Sol. :
import
java.util.*;
public
class Example
{
public
static void main(String[] args)
{
Scanner
scanner = new Scanner(System.in);
System.out.print("Enter
the price: ");
float
price = scanner.nextFloat();
System.out.print("Enter
the quantity: ");
int
quantity= scanner.nextInt();
float
amount;
amount=price*
quantity;
System.out.println("\nPrice:
"+price);
System.out.println("Quantity:
"+quantity);
System.out.println("------------------------");
System.out.println("Total
Bill in Rs: "+amount);
System.out.println("-----------------------");
}
}
Output
Enter
the price: 20.50
Enter
the quantity: 10
Price:
20.5
Quantity:
10
-----------------------------
Total
Bill in Rs: 205.0
------------------------------
The
simple method used for writing the output on the console is write(). Here is
the syntax of using write() method -
System.out.write(int
b)
The
bytes specified by b has to be written on the console. But typically print() or
println() is used to write the output on the console. And these methods belong
to PrintWriter class.
Object Oriented Programming: Unit IV: I/O, Generics, String Handling : Tag: : Syntax with Example Java Programs - Reading and Writing Console I/O
Object Oriented Programming
CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation