Introduction to Java User Input
Sometimes, while writing code, we may want the user to input different values or we want to take the specified data type value from the user. In different programming languages, there are different methods to take input from the user. In java, we have three different kinds of methods through which we can take inputs from the user. In this tutorial, we will learn about different methods that are available in java to take input from users including Scanner class, Buffered class, and console class. But our main focus will be the Scanner class because it is the most used and popular one. Moreover, we will cover various cases and examples of taking the input from the user.
All in all, this tutorial will contain all the necessary information and examples that you need to know in order to get input from users in different scenarios.
Getting started with Java User Input
In the Java programming language, there are various ways to get input from the user. For example, using scanner class which is the most popular one, using Buffered class, and using the console. In this tutorial, we will cover all three methods but our focus will be on the scanner class and we will take various examples using scanner class. This tutorial requires basic knowledge of different data types that are available in java because we will be taking the input from users in different data formats. So, if you are not familiar with data types, you can learn from the article java data types.
Method-1: Java user input using Scanner class
The Scanner class is the most preferred method to take input from the user in the java programming language. The scanner class in java.util
package used for obtaining the input of the primitive types like int, double, etc., and strings. It is the easiest way to read input in a Java program, though not very efficient if you want an input method for scenarios where time is a constraint like in competitive programming.
Syntax of Java Scanner Class
Now we already know the basic knowledge about the Scanner class, let us now see the syntax of the Java scanner class. The simple syntax of the Java Scanner class looks like this:
Scanner input = new Scanner(System.in);
Here we initiate the Scanner class and create a new object type named input. We can give any name but later we have to use the same name to take input from the user. Now, we had created a new object type Scanner, we can take different types of inputs from the user. For example to take an integer as an input from the user we use the following program.
input.nextInt();
And to get the String as an input from the user we will use the following syntax.
input.next();
We used the name "input
" because we have created a scanner object with this name. We can create our scanner object with any name, but then we have to use the same name while getting inputs from the user.
Methods of Java Scanner Class
As we already have seen that we used different methods to get a string data type and an int data type from the user. In a similar way, we have different methods available in the Scanner class which is used to take different data types from the user. See the table below, which shows some of those methods and a little description of them.
Methods | Description |
---|---|
nextInt() | It takes int type input value from the user. |
nextFloat() | It takes a float type input value from the user. |
nextBoolean() | It takes a boolean type input value from the user. |
nextLine() | t takes a line as an input value from the user. |
next() | It takes a word as an input value from the user. |
nextByte() | It takes a byte type of input value from the user. |
nextDouble() | It takes a double type input value from the user. |
nextShort() | It takes a short type input value from the user. |
nextLong() | It takes a long type of input value from the user. |
Example-1 Taking string as an input from the user
Now, we already know the syntax of the Scanner class and know some of the methods that are available in the Scanner class. Let us now take an example and see how we can get a string as an input from the user and then will print that string from the user. See the example below:
// importing Scanner class
import java.util.Scanner;
// java main class
public class Main{
// java main method
public static void main(String args[]){
// creating Scanner object
Scanner input = new Scanner(System.in);
// java user input of string type
String name = input.nextLine();
// printing the java use input
System.out.println("Your name is: "+name);
}
}
Output:
bashir
Your name is: bashir
Here the first line is not the output. It is actually the input that we provide to our Scanner class. The java code will not execute further until we provide the string/input. You will get a different name as output if you provide a different name as input.
Example-2 Taking integer as an input from the user
Now let us take an example of an integer value and take an int value as an input from the user.
// importing scanner class
import java.util.Scanner;
// java main class
public class Main{
// java main method
public static void main(String args[]){
// creating Scanner object
Scanner input = new Scanner(System.in);
// printing
System.out.println("Enter your age!");
// java user input of Int type
Integer age = input.nextInt();
// printing the java use input
System.out.println("Your age is: "+age);
}
}
Output:
Enter your age!
22
Your age is: 22
Notice that we specified the input data type before getting the input from the user. If we will get an input of any other data type from the user, it will return an error. See the example below:
// importing scanner class
import java.util.Scanner;
// java main class
public class Main{
// java main method
public static void main(String args[]){
// creating Scanner object
Scanner input = new Scanner(System.in);
// printing
System.out.println("Enter your age!");
// java user input of Int type
Integer age = input.nextInt();
// printing the java use input
System.out.println("Your age is: "+age);
}
}
Now instead of inputting my age, I will just put my name which is a string value.
Notice that when the program asks for my age, I just enter my name which was a string data type and we get the above error.
Example-3 Taking floating point as an input from the user
The decimal points are called floating points. Now let us take the floating-point as an input from the user and print it out. See the example below:
// importing scanner class
import java.util.Scanner;
// java main class
public class Main{
// java main method
public static void main(String args[]){
// creating Scanner object
Scanner input = new Scanner(System.in);
// printing
System.out.println("Enter you marks!");
// java user input of float type
Float marks = input.nextFloat();
// printing the java use input
System.out.println("Your age is: "+marks);
}
}
Output:
Enter you marks!
85.54
Your age is: 85.54
Notice that we successfully took floating point as an input from the user and print it without any error.
Method-2: Java user input using Buffered class
Another way to take input from the user is using the Buffered class. It is a simple class that is used to read a sequence of characters. It has simple functions that read a character, an array of characters, and a readLine()
function which reads a line. InputStreamReader()
is a function that converts the input stream of bytes into a stream of characters so that it can be read as BufferedReader
expects a stream of characters. In this section, we will look at the basic syntax of the Buffered class and will take an example as well.
Syntax of Buffered class to take user input
So now, we have some idea of Buffered class. Let us now see the syntax of the Buffered class to take input from the user. See the syntax below:
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
Notice that we gave System.in
as an argument which means this method is going to take input from the user.
Example-1 Taking String value from the user using BufferedReader class
Now we already know the syntax of the BufferedReader
class and creating the input from the user. Let us now take an example, take String value as an input and print out that value. See the example below:
// importing BufferedReader class
import java.io.BufferedReader;
// Importing inputStreamReader class
import java.io.InputStreamReader;
// java main class
public class Main{
// java main method
public static void main(String args[]){
// creating input for user using bufferedReader class
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
// printing
System.out.println("Input your name: ");
try{
// java user input of String data type
String String_name = input.readLine();
System.out.println("Your name is : " +String_name);
}
catch (Exception e) {
// printing the exception
System.out.println("Please enter String type");
}
}
}
Output:
Input your name:
Bashir
Your name is : Bashir
Notice that we have written our code inside the try and catch block which is used to catch errors. You can read more about try and catch blocks from the article java try and catch exceptions.
Example-2 Taking integer as an input using the BufferedReader class
Now let us take integer value as an input using the BufferedReader class. It is a little bit different than taking string as an input. All the code will be the same as the above one, except we use the Integer class and parseInt()
method. See the example below:
// importing BufferedReader class
import java.io.BufferedReader;
// Importing inputStreamReader class
import java.io.InputStreamReader;
// java main class
public class Main{
// java main method
public static void main(String args[]){
// creating input for user using bufferedReader class
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
// printing
System.out.println("Input your age: ");
try{
// java user input of Int data type
int Integer_value = Integer.parseInt(input.readLine());
System.out.println("Your age is: "+ Integer_value);
}
catch (Exception e) {
// printing the exception
System.out.println("Please enter Int type");
}
}
}
Output:
Input your age:
22
Your age is: 22
Notice that we were able to take integer values as an input from the user using the Java bufferedReader class.
Method-3: Java user input using console class
The Java Console class is the third technique to take input from the user through the console. The Console class was introduced in Java 1.5 and onward. This class is present in the java.io package
. There are several methods in the Console class that help in reading input texts and passwords from the console, without displaying them on the screen. Secondly, we also use this Console class to read password-like input without displaying the characters of the input password on the console.
Syntax of console class to take input from the user
The syntax of the console class to take input from the user is very simple as we don't need to create an input object. We just need to call the console method. For example, the simple syntax to take string value from the user will looks like this.
String Name = System.console().readLine();
The above code will take string data typed input from the user and will store it in the variable named "Name".
Example of taking input from user using Java console class
Now we know the basic syntax of the java console class and know how to take input from the user. Here we will take an example and see how we use it in our real programming. See the example below:
// java main class
public class Main{
// java main method
public static void main(String args[]){
// printing
System.out.println("Input your name:");
// java user input using console class
String Name = System.console().readLine();
// printing the user name
System.out.println("Your name is :"+Name);
}
}
Output:
Input your name:
Bashir
Your name is :Bashir
Notice that we get the user input using the console class and displayed it using the println
method.
Summary
In programming languages, sometimes we need to take the input data from the user. For example, entering a password from the user or their user name. In this tutorial, we learned how we can take input from the user in multiple ways. We covered three different methods to take input from the user including Scanner class, BufferedReader class, and Console class. We take various cases in each of the methods and take different types of data from users using these classes. All in all, this tutorial contains all the methods that are used to take input in java.
Further Reading
Java user input
Console class
Java BufferedReader class