In the following program, we will see data on how to take input from Users in Java using the following methods:
- BufferedReader
- Reading input data using Data InputStreamReader
- Reading input data using Scanner
Also, you can use the Java compiler to compile a program.
1. Buffered Reader class.
This is the Java classical method to take input. This 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 user in the command line.
Advantages:
- The input is buffered for efficient reading.
Drawback:
- The wrapping code is hard to remember.
Java BufferedReader class Example
Roll number, name, salary take Input from Users.
SOURCE CODE:
import java.io.*;
class Rbuf
{
public static void main(String args[]) throws IOException
{
int rollno;
String name;
float salary;
BufferedReader s=new BufferedReader(new
InputStreamReader(System.in));
//Create object
System.out.println("Enter the Roll no:");
rollno=Integer.parseInt(s.readLine()); //Input
System.out.println("Enter the Name:");
name=s.readLine(); //Input
System.out.println("Enter the Salary:");
salary=Float.parseFloat(s.readLine()); //Input
System.out.println("Roll no:"+rollno);
System.out.println("Name:"+name);
System.out.println("Salary:"+salary);
} //End of main
} //End of classimport java.io.*;
OUTPUT:
C:\A>javac Rbuf.java
C:\A>java Rbuf
Enter the Roll no:
59
Enter the Name:
SAM
Enter the Salary:
500
Roll no:59
Name:SAM
Salary:500.0
2. Data input Scream Class
Java DataInputStream class allows an application to read primitive data from the input stream in a machine-independent way. Java application generally uses the data output stream to write data that can later be read by a data input stream.
Data input Scream Class Example
Take roll number, name, salary from user as an input.
SOURCE CODE:
import java.io.*;
class Rdiss
{
public static void main(String args[]) throws IOException
{
int rollno;
String name;
float salary;
DataInputStream s=new DataInputStream(System.in); //Create
object
System.out.println("Enter the Roll no:");
rollno=Integer.parseInt(s.readLine()); //Input
System.out.println("Enter the Name:");
name=s.readLine(); //Input
System.out.println("Enter the Salary:");
salary=Float.parseFloat(s.readLine()); //Input
System.out.println("Roll no:"+rollno);
System.out.println("Name:"+name);
System.out.println("Salary:"+salary);
} //End of main
} //End of class
OUTPUT:
C:\A>javac Rdiss.java
C:\A>java Rdiss
Enter the Roll no:
01
Enter the Name:
SAM
Enter the Salary:500
Roll no:1
Name:SAM
Salary:500.0
3. Scanner class
The Scanner
class is used to get user input, and it is found in the java.util
package.
To use the Scanner
class, create an object of the class and use any of the available methods found in the scanner class documentation. In our example, we will use the nextLine()
method, which is used to read strings
Scanner class Class Example
Take roll number, name, salary from user as an input.
Source Code
import java.util.*;
class Rscanner
{
public static void main(String args[])
{
int rollno;
String name;
float salary;
Scanner s=new Scanner(System.in); //Create object
System.out.println("Enter the Roll no:");
rollno=s.nextInt(); //Input
System.out.println("Enter the Name:");
name=s.next(); //Input
System.out.println("Enter the Salary:");
salary=s.nextFloat(); //Input
System.out.println("Roll no:"+rollno);
System.out.println("Name:"+name);
System.out.println("Salary:"+salary);
} //END OF MAIN
} // END OF CLASS
OUTPUT.
C:\A>javac Rscanner.java
C:\A>java Rscanner
Enter the Roll no:
01
Enter the Name:
SAM
Enter the Salary:
500
Roll no:1
Name:SAM
Salary:500.0
Also, View: – Write a program to create a registration form using AWT.
Also, View- Write a java program to create a window using swing.
This Post is very cool and more informative to me. Thanks for posting it.