0% found this document useful (0 votes)
20 views8 pages

Java IO

The document provides an overview of input and output handling in Java, focusing on the Scanner and BufferedReader classes for user input. It explains how to create objects of these classes, read different data types, and the differences between various output methods like print(), println(), and printf(). Additionally, it includes examples and assignments to practice reading and displaying different data types.

Uploaded by

Puja Kumari
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)
20 views8 pages

Java IO

The document provides an overview of input and output handling in Java, focusing on the Scanner and BufferedReader classes for user input. It explains how to create objects of these classes, read different data types, and the differences between various output methods like print(), println(), and printf(). Additionally, it includes examples and assignments to practice reading and displaying different data types.

Uploaded by

Puja Kumari
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

Input and Output In Java

Java Input-Scanner class


Java provides different ways to get input from the user. However, we will learn to
get input from user using the object of Scanner class.
In order to use the object of Scanner, we need to import [Link] package.

import [Link]; // importing only the Scanner class from util package
import [Link].*;//importing all the class from util package

Then, we need to create an object of the Scanner class. We can use the object to
take input from the user.

// create an object of Scanner


Scanner input = new Scanner([Link]); //The “[Link]” represents the keyboard.

1|P a ge
Example1:

import [Link].*;//util package contains Scanner Class

class InputDemo1

{ public static void main(String args[])

//Creating object of scanner and passing [Link] as parameter to read


//from the keyboard.

Scanner sc=new Scanner([Link]);

[Link]("Enter Two Integer Number:");

//nextInt()method to read integer value.

int a=[Link]();

int b=[Link]();

int sum=a+b;

[Link]("SUM="+sum);

[Link]("Enter Two Float Number:");

//nextInt()method to read integer value.

float f1=[Link]();

float f2=[Link]();

float fsum=f1+f2;

[Link]("SUM of two Float="+fsum);

//nextLine()method to read string

[Link]("Enter a String:");

String str=[Link]();

[Link](str);

2|P a ge
BufferedReader – Java class
BufferedReader is a Java class that reads text from the input stream. It buffers the characters
so that it can get the efficient reading of characters, arrays, etc. It inherits the reader class and
makes the code efficient since we can read the data line-by-line with the readline() method.

Public class BufferedReader extends Reader

There are a few pointers we have to keep in mind while working with BufferedReader class in
Java.

 We may have to specify the buffer size even though the default is large enough for any
purpose.
 With each request made of a reader a corresponding, a read request is also made of an
underlying character.
 It is always advised to wrap a BufferedReader class around any reader such as
InputStreamReaders.
 For the programs that use DataInputaStreams for textual input, an appropriate
BufferedReader replaces the DataInputStream to localize it.

Java BufferedReader Constructors


Constructor Description
This constructor creates a buffering character-
BufferedReader(Reader reader) input stream that works on a default-size input
buffer.
It uses the specified size for the input buffer for
BufferedReader(Reader reader, int size)
buffering the character-input stream.

3|P a ge
Create a BufferedReader

In order to create a BufferedReader , we must import the [Link] package


first. Once we import the package, here is how we can create the reader.

Reading Data From Console By InputStreamReader And


BufferedReader in Java

Example2:
class InputDemo2
{
public static void main(String args[])
{
Bufferedreader bufferedreader = new BufferedReader(
New InputStreamReader([Link]));
[Link](“enter name”);
String name = [Link]();
[Link](“enter age”);
int age = [Link]([Link]());
int age1= [Link]();

4|P a ge
[Link](“I am” + name + “ “+age+”years old”);}}

Java Output
In Java, you can simply use to send output to standard output (screen).
[Link](); or

[Link](); or

[Link]();

Here,

 System is a class
 out is a public static field:
 it accepts output data.

Difference between println(), print() and printf()

 print() - It prints string inside the quotes.


 println() - It prints string inside the quotes similar like print() method. Then the
cursor moves to the beginning of the next line.
 printf() - Tt provides string formatting (similar to printf in C/C++ programming).

5|P a ge
Example 3: print() and println()

class Output {
public static void main(String[] args) {

[Link]("1. println ");


[Link]("2. println ");

[Link]("1. print ");


[Link]("2. print");
}
}

Example 4: Printing Variables and Literals

class Variables {
public static void main(String[] args) {

Double number = -10.6;

[Link](5);
[Link](number);
}
}

Example 5: Print Concatenated Strings

class PrintVariables {
public static void main(String[] args) {

Double number = -10.6;

[Link]("I am " + "awesome.");


[Link]("Number = " + number);
}
}

6|P a ge
Assignment1
1. WAP in Java to read and display integer, float, double, boolean, String
and byte using Scanner Class.
2. WAP in Java to read and display Roll, Name, Class, and subject using
BufferedReader Class.

More on input method of Scanner class : next() and nextLine() methods

Example 6: Java Scanner next()

import [Link];

class Main {
public static void main(String[] args) {

// creates an object of Scanner


Scanner input = new Scanner([Link]);
[Link]("Enter your name: ");

// reads the entire word


String value = [Link]();
[Link]("Using next(): " + value);

[Link]();
}
}

Output

Enter your name: Ritesh Kumar


Using next(): Ritesh

Note:-In the above example, we have used the next() method to read a string
from the user. Here, we have provided the full name. However, the next() method
only reads the first name. This is because the next() method reads input up to
the whitespace character. Once the whitespace is encountered, it returns the
string (excluding the whitespace).

7|P a ge
Example 7: Java Scanner nextLine()

import [Link];

class Main {
public static void main(String[] args) {

// creates an object of Scanner


Scanner input = new Scanner([Link]);
[Link]("Enter your name: ");

// reads the entire line


String value = [Link]();
[Link]("Using nextLine(): " + value);

[Link]();
}
}

Output

Enter your name: Ritesh Kumar


Using nextLine(): Ritesh Kumar

Note:-In the example7, we have used the nextLine() method to read a string from
the user. Unlike next(), the nextLine() method reads the entire line of input
including spaces. The method is terminated when it encounters a next line
character, \n.

8|P a ge

You might also like