How to get input from user in Java
Java Scanner Class
The Scanner class is used to get user input, and it is found in the [Link] package.
Syntax:
Scanner sc=new Scanner([Link]);
To use the Scanner class, create an object of the class and use any of the available methods
found in the Scanner class documentation.
Ex1: In our example, we will use the nextLine() method, which is used to read Strings:
import [Link]; // Import the Scanner class
class demo {
public static void main(String[] args) {
Scanner myObj = new Scanner([Link]); // Create a Scanner object
[Link]("Enter username:");
String userName = [Link](); // Read user input
[Link]("Username is: " +userName); // Output user input
}
}
o/p:
Enter username
Gireesh
Username is: Gireesh
Input Types:
In the example above, we used the nextLine() method, which is used to read Strings. To read
other types, look at the table below:
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()/next() Reads a String value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the user
Ex2: In the example below, we use different methods to read data of various types:
import [Link];
class demo {
public static void main(String[] args) {
Scanner myObj = new Scanner([Link]);
[Link]("Enter name, age and salary:");
// String input
String name = [Link]();
// Numerical input (Integer and double value)
int age = [Link]();
double salary = [Link]();
// Output input by user
[Link]("Name: " + name);
[Link]("Age: " + age);
[Link]("Salary: " + salary);
//[Link](name + " is of type " + ((Object)name).getClass().getSimpleName());
//[Link](age + " is of type " + ((Object)age).getClass().getSimpleName());
//[Link](salary+ " is of type " +((Object)salary).getClass().getSimpleName());
}
}
Note: If you enter wrong input (e.g. text in a numerical input), you will get an exception/error
message (like "InputMismatchException").
Example of integer input from user
The following example allows user to read an integer form the [Link].
1. import [Link].*;
2. class UserInputDemo
3. {
4. public static void main(String[] args)
5. {
6. Scanner sc= new Scanner([Link]); //[Link] is a standard input stream
7. [Link]("Enter first number- ");
8. int a= [Link]();
9. [Link]("Enter second number- ");
10. int b= [Link]();
11. [Link]("Enter third number- ");
12. int c= [Link]();
13. int d=a+b+c;
14. [Link]("Total= " +d);
15. }
16. }
Example of String Input from user
Let's see another example, in which we have taken string input.
1. import [Link].*;
2. class UserInputDemo1
3. {
4. public static void main(String[] args)
5. {
6. Scanner sc= new Scanner([Link]); //[Link] is a standard input stream
7. [Link]("Enter a string: ");
8. String str= [Link](); //reads string
9. [Link]("You have entered: "+str);
10. }
11. }
Note: