In this tutorial, we will learn how to get input from the user in the Java programming language.
There are two ways to get input from the user in the Java programming language:
1. Java Scanner Class
2. Java BufferedReader Class
1. Java Scanner Class
Ã
Java Scanner Class allows the user to take input from the console.
à It
belongs to the ‘java.util’ package.
à It
is used to read the input of primitive type, like: int, float, double, long,
short, etc.
à It
is the easiest way to read input in Java program, but not recommended.
Syntax
Scanner sc = new Scanner (System.in);
Methods of Java Scanner Class
Java Scanner Class provides the following
methods to read different primitive types:
Method
|
Description
|
nextBoolean()
|
Reads a boolean value from the user
|
nextByte()
|
Reads a byte value from the user
|
nextDouble()
|
Reads a double value from the user
|
nextFloat()
|
Reads a float value from the user
|
nextInt()
|
Reads a int value from the user
|
nextLine()
|
Reads a String value from the user
|
nextLong()
|
Reads a long value from the user
|
nextShort()
|
Reads a short value from the user
|
Example
/*
The following example allows user to enter three integer values from the
keyboard and claculate the sum of the entered numbers using Java Scanner Class.
Date: Tuesday, 28-01-2020
@author: Ankur Saxena
*/
//importing package
import java.util.Scanner;
public class UserInputDemo1{
public static void main (String[] args){
//creating new scanner class
Scanner sc = new Scanner (System.in); //'System.in' is a standard input stream
System.out.println ("Please enter first number: ");
int num1 = sc.nextInt();
System.out.println ("Please enter second number: ");
int num2 = sc.nextInt();
System.out.println ("Please enter third number: ");
int num3 = sc.nextInt();
//calculation
int sum = num1+num2+num3;
//printing result
System.out.println ("Sum: "+sum);
}
}
Save this file as 'UserInputDemo1.java'
Compile: $javac UserInputDemo1.java
Execute: $java UserInputDemo1
Output:
Please enter first number:
20
Please enter second number:
30
Please enter third number:
40
Sum: 90
2. Java BufferedReader Class
Ã
Java BufferedReader Class is used to read the text from a character based input
stream.
à It
can be used to read data line by line by ‘readLine()’ method.
à It
makes the performance fast.
à It
belongs to the ‘java.io’ package.
à It
inherits ‘Reader Class’.
Syntax
InputSreamReader is = new InputStreamReader
(System.in);
BufferedReader br = new BufferedReader (is);
Example
/*
The following example allows user to enter three integer values from the
keyboard and claculate the sum of the entered numbers using Java BufferedReader Class.
Date: Tuesday, 28-01-2020
@author: Ankur Saxena
*/
//importing package
import java.io.BufferedReader;
import java.io.InputStreamReader; //recommanded method
import java.io.IOException;
//import java.io.*;//importing all packages not recommanded
public class UserInputDemo2{
public static void main (String[] args) throws IOException
{
int num1, num2, num3;
int total;
//creating new InputStreamReader
InputStreamReader is = new InputStreamReader (System.in);
//creating new BufferedReader Class
BufferedReader br = new BufferedReader (is);
/*Method-2:
Using BufferedReader and InputStreamReader in single command:
BufferedReader in = new BufferedReader (new InputSreamReader (System.in));
*/
System.out.println ("Please enter any three integer numbers:\n");
num1 = Integer.parseInt (br.readLine());
num2 = Integer.parseInt (br.readLine());
num3 = Integer.parseInt (br.readLine());
//calculation
total = num1+num2+num3;
//printing result
System.out.println ("Sum of "+num1+", "+num2+" and "+num3+" is = "+total);
}
}
Save this file as 'UserInputDemo2.java'
Compile: $javac UserInputDemo2.java
Execute: $java UserInputDemo2
Output:
Please enter any three integer numbers:
15
15
15
Sum of 15, 15 and 15 is = 45
0 Comments